重复的列值 ,只显示第一行,其他的显示为空。
这样的一个数据的排列,
col1 col2 col3
1 1 2
1 1 3
1 1 4
2 2 5
2 2 6
2 3 7
显示成这样的
col1 col2 col3
1 1 2
3
4
2 2 5
6
2 3 7
请教这语法?多谢!
[解决办法]
select col1=(case when exists(select 1 from tb t2 where t1.col1=t2.col1 and t1.col2=t2.col2 and t1.col3>t2.col3) then '' else rtrim(col1) end),
col2=(case when exists(select 1 from tb t2 where t1.col1=t2.col1 and t1.col2=t2.col2 and t1.col3>t2.col3) then '' else rtrim(col2) end),col3 from tb t1
[解决办法]
select *,IDENTITY(int,1,1) col3 into #t from
(
select 1 col1,1 col2
union all select 1,1
union all select 1,1
union all select 2,2
union all select 2,2
union all select 2,3
)a
select case when rn=1 then convert(varchar,col1) else '' end col1
,case when rn=1 then convert(varchar,col2) else '' end col2
,col3
from (select *,(select COUNT(1) from #t where col1=a.col1 and col2=a.col2 and col3<=a.col3)rn from #t a)a
/*
col1 col2 col3
------------------------------ ------------------------------ -----------
1 1 1
2
3
2 2 4
5
2 3 6
*/