日期:2014-05-17 浏览次数:20758 次
;WITH A AS
(
SELECT PX=ROW_NUMBER()OVER(PARTITION BY 列一 ORDER BY 列二),* FROM TB
)
SELECT
CASE WHEN PX=1 THEN 列一 ELSE '' END AS 列一,列二,列三
FROM
F
--> 测试数据:[TB]
if object_id('[TB]') is not null drop table [TB]
GO
create table [TB]([列一] varchar(2),[列二] varchar(1),[列三] int)
insert [TB]
select '01','a',1 union all
select '01','b',1 union all
select '02','c',2 union all
select '02','d',2 union all
select '02','e',2 union all
select '03','f',3
WITH temp AS(
select *,flag=ROW_NUMBER() OVER (PARTITION BY [列一] order by [列二]) from [TB]
)
SELECT
[列一]=CASE WHEN flag=1 THEN [列一] ELSE '' END,
[列二],
[列三]=CASE WHEN flag=(select max(flag) from temp WHERE t.[列一]=[列一]) then RTRIM([列三]) else '' end
FROM temp t
/*
列一 列二 列三
---- ---- ------------
01 a
b 1
02 c
d
e 2
03 f 3
(6 行受影响)
*/
drop table [TB]