日期:2014-05-18 浏览次数:20711 次
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (名称 varchar(50),XXX int)
insert into tb values('A',100)
insert into tb values('A',200)
insert into tb values('C',200)
insert into tb values('d',100)
insert into tb values('A',500)
insert into tb values('C',200)
select (case when xxx = (select top 1 xxx from tb where 名称 = t.名称) then 名称 else '' end) 名称, xxx from tb t
名称 xxx
-------------------------------------------------- -----------
A 100
200
C 200
d 100
500
C 200
(6 行受影响)
------解决方案--------------------
if OBJECT_ID('tb') is not null
drop table tb
go
create table tb (名称 varchar(50),XXX int)
insert into tb values('A',100)
insert into tb values('A',200)
insert into tb values('C',200)
insert into tb values('d',100)
insert into tb values('A',500)
insert into tb values('C',200)
select 名称=(case when rn=1 then 名称 else ''end ),XXX
from (select *,rn=ROW_NUMBER()over( partition by 名称 order by getdate()) from tb) t
名称 XXX
-------------------------------------------------- -----------
A 100
200
500
C 200
200
d 100
(6 行受影响)