首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

反复的列值 ,只显示第一行,其他的显示为空

2013-04-07 
重复的列值 ,只显示第一行,其他的显示为空。这样的一个数据的排列,col1col2col3112113114225226237显示成这

重复的列值 ,只显示第一行,其他的显示为空。
这样的一个数据的排列,
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
*/

热点排行