sql一列多行转为一行多列

秦拿希 2016-04-15 04:37:36
有这样的需求

表名test
id value
-------------
1 a
1 b
1 c
1 d
1 e

要把上面的结果集转为一行多列,如下
id value1 value2 value3 value4 value5
1 a b c d e

普通sql无法处理,求教大神,写存储过程拼接sql是否可以,如何实现?提供思路,谢谢

...全文
2399 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wang_zhm 2020-05-20
  • 打赏
  • 举报
回复
太棒了,解决了我的大问题啊,再次感谢楼上各位哈!
fishparadise 2016-04-16
  • 打赏
  • 举报
回复
都给版主们包了。
中国风 2016-04-15
  • 打赏
  • 举报
回复
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		
*/
Ekun_sky 2016-04-15
  • 打赏
  • 举报
回复
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
-----------------------
*/
 
 
唐诗三百首 2016-04-15
  • 打赏
  • 举报
回复

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)
*/

22,294

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧