日期:2014-05-17 浏览次数:20400 次
with a1 (id,[value]) as
(
select 1 ,'a' union all
select 2 ,'a' union all
select 3 ,'b' union all
select 4 ,'a' union all
select 5 ,'a' union all
select 6 ,'a' union all
select 7 ,'a' union all
select 8 ,'b' union all
select 9 ,'a' union all
select 10 ,'a'
)
,a2 as
(
select *,id-row_number() over(partition by [value] order by id) re
from a1
)
,a3 as
(
select distinct b.id,rtrim(a.[value])+rtrim(b.id)+'-'+rtrim(b.n) [value]
from a2 a
cross apply
(
select min(id) id,count(*) n from a2 where [value]=a.[value] and re=a.re
) b
)
,a4 as
(
select [value],'c'+rtrim(row_number() over(order by id)) colName
from a3
)
select *
from a4
pivot (max([value]) for colName in ([c1],[c2],[c3],[c4],[c5],[c6],[c7])) pvt