22,294
社区成员
发帖
与我相关
我的任务
分享

create table #test
(id int, value varchar(5))
insert into #test
select 1, 'a' union all
select 1, 'b' union all
select 1, 'c' union all
select 1, 'd' union all
select 1, 'e' union all
select 2, 'b' union all
select 2, 'c' union all
select 2, 'd'
go
declare @Sql nvarchar(max)='',@Cols varchar(3);
select top 1 @Cols=count(*) from #test group by ID order by count(*) desc;
while @Cols>0
select @Sql=',[value'+@Cols+']=max(case when RN='+@Cols+' then value else '''' end )'+@Sql,@Cols=@Cols-1
exec('select ID'+@Sql+' from (select *,RN=ROW_NUMBER()over(partition by ID order by ID) from #test) as T group by ID')
/*
ID value1 value2 value3 value4 value5
1 a b c d e
2 b c d
*/use master
go
if object_id('test') is not null drop table test
create table test(id int, value varchar(15))
insert into test
select 1, 'a' union all
select 1, 'b' union all
select 1, 'c' union all
select 1, 'd' union all
select 1, 'e'
declare @i varchar(500),@j varchar(500)=''
select @i=isnull(@i,'')+',['+ltrim(aid)+']' from
(select ROW_NUMBER()over(order by getdate()) as aid,* from test) as b
set @j='select * from
(select ROW_NUMBER()over(order by getdate()) as aid,* from test) as b pivot(max(value) for aid in ('+
stuff(@i,1,1,'')+')) as c'
exec(@j)
/*
----------------------
1 a b c d e
-----------------------
*/
create table test
(id int, value varchar(5))
insert into test
select 1, 'a' union all
select 1, 'b' union all
select 1, 'c' union all
select 1, 'd' union all
select 1, 'e'
select id,value1=[1],value2=[2],value3=[3],value4=[4],value5=[5]
from (select id,value,row_number() over(partition by id order by getdate()) 'rn' from test) a
pivot(max(value) for rn in ([1],[2],[3],[4],[5])) p
/*
id value1 value2 value3 value4 value5
----------- ------ ------ ------ ------ ------
1 a b c d e
(1 row(s) affected)
*/