22,209
社区成员
发帖
与我相关
我的任务
分享
--测试数据
create table 表(a varchar(10),b varchar(10),d varchar(10))
insert 表 select '1','101','222'
union all select '2','101','302'
union all select '3','101','205'
union all select '4','102','203'
union all select '5','102','560'
union all select '6','101','302'
go
--处理1
select id=identity(int,1,1),* into #t from 表
select a,b=case id when (select min(id) from #t where b=a.b)
then b else '' end
,d
from #t a
/*
a b d
---------- ---------- ----------
1 101 222
2 302
3 205
4 102 203
5 560
6 302
(所影响的行数为 6 行)
*/
drop table #t
go
--处理2
select a,
b=case when exists(select 1 from 表 where b=t.b and a<t.a) then '' else b end,
d
from 表 T
order by a,b
/*
a b d
---------- ---------- ----------
1 101 222
2 302
3 205
4 102 203
5 560
6 302
(所影响的行数为 6 行)
*/
--删除测试环境
drop table 表