select identity(int,1,1) as id,名称,日期,价格 into #t from 表名 group by 名称,日期,价格select 名称=case when id in (select min(id) from #t group by 名称) then 名称 else '' end ,日期,价格 from #t drop table #t
可以建立一个唯一索引ID IDENTITY(INT,1,1) select 名称=(case when ID in(select min(ID) from test group by 名称 ) then 名称 else '' end) ,日期,价格 from test a order by 日期 desc,名称
搞错,改一下: select identity(int,1,1) as id,名称,日期,价格 into #t from 表名 select 名称=case when id in (select min(id) from #t group by 名称) then 名称 else '' end ,日期,价格 from #t drop table #t
select 名称=(case when 日期+价格 in (select min(日期+价格) from test group by 名称) then 名称 else '' end) ,日期,价格 from test a order by 日期 desc,名称
select 名称=(case when cast(日期 as varchar)+cast(价格 as varchar) in (select min(cast(日期 as varchar)+cast(价格 as varchar)) from test group by 名称) then 名称 else '' end) ,日期,价格 from test a order by 日期 desc,名称
select 名称=(case when 价格 in(select min(价格) from test group by 名称) then 名称 else '' end) ,日期,价格 from test a order by 日期 desc,名称
TO chinaandys(风流泪&VS&雨含笑) 谢谢,我试了一下,不对呀
select identity(int,1,1) as id,名称,日期,价格 into #t from 表名 --不要排序select 名称=(case when id in (select min(id) from #t group by 名称) then 名称 else '' end) ,日期,价格 from #t drop table #t
create table tb(名称 varchar(10),日期 datetime,价格 int) insert into tb select '搜狐','2004-12-22',12 union all select '网易','2004-12-30',18 union all select '网易', '2004-12-29',20 union all select '搜狐', '2004-12-24',22 union all select '搜狐', '2004-12-25',25 union all select '新浪', '2004-12-29',52 union all select '网易','2004-12-21',100 select identity(int,1,1) as id,名称,日期,价格 into #t FROM tb ORDER BY 名称,日期select (case when id in (select min(id) from #t group by 名称) then 名称 else '' end) AS 名称 ,日期,价格 FROM #t --不要排序 搜狐 2004-12-22 00:00:00.000 12 2004-12-24 00:00:00.000 22 2004-12-25 00:00:00.000 25 网易 2004-12-21 00:00:00.000 100 2004-12-29 00:00:00.000 20 2004-12-30 00:00:00.000 18 新浪 2004-12-29 00:00:00.000 52
谢谢!!
按你说的方法,如果表中很多名称,不是要写N个UNION语句??
有其它方法吗?
go
select 名称=(case when 价格 in(select min(价格) from test group by 名称
) then 名称 else '' end)
,日期,价格
from test a
,日期,价格
from test a order by 日期 desc,名称
into #t
from 表名
group by 名称,日期,价格select 名称=case
when id in (select min(id) from #t group by 名称) then 名称
else ''
end
,日期,价格
from #t
drop table #t
select 名称=(case when ID in(select min(ID) from test group by 名称 ) then 名称 else '' end)
,日期,价格
from test a order by 日期 desc,名称
select identity(int,1,1) as id,名称,日期,价格
into #t
from 表名 select 名称=case
when id in (select min(id) from #t group by 名称) then 名称
else ''
end
,日期,价格
from #t
drop table #t
,日期,价格
from test a order by 日期 desc,名称
,日期,价格
from test a order by 日期 desc,名称
,日期,价格
from test a order by 日期 desc,名称
谢谢,我试了一下,不对呀
into #t
from 表名 --不要排序select 名称=(case
when id in (select min(id) from #t group by 名称) then 名称
else ''
end)
,日期,价格
from #t
drop table #t
insert into tb
select '搜狐','2004-12-22',12
union all select '网易','2004-12-30',18
union all select '网易', '2004-12-29',20
union all select '搜狐', '2004-12-24',22
union all select '搜狐', '2004-12-25',25
union all select '新浪', '2004-12-29',52
union all select '网易','2004-12-21',100
select identity(int,1,1) as id,名称,日期,价格 into #t FROM tb ORDER BY 名称,日期select (case when id in (select min(id) from #t group by 名称) then 名称 else '' end) AS 名称
,日期,价格
FROM #t --不要排序
搜狐 2004-12-22 00:00:00.000 12
2004-12-24 00:00:00.000 22
2004-12-25 00:00:00.000 25
网易 2004-12-21 00:00:00.000 100
2004-12-29 00:00:00.000 20
2004-12-30 00:00:00.000 18
新浪 2004-12-29 00:00:00.000 52