表A
a b
-------------
a a1
a a2
a a3
a a4
a a5
a a6
b b1
b b2
b b3
b b4
b b5
b b6得到结果:
a b
-------------
a a1,a2,a3,a4,a5,a6
b b1,b2,b3,b4,b5,b6语句实现,谢谢!
a b
-------------
a a1
a a2
a a3
a a4
a a5
a a6
b b1
b b2
b b3
b b4
b b5
b b6得到结果:
a b
-------------
a a1,a2,a3,a4,a5,a6
b b1,b2,b3,b4,b5,b6语句实现,谢谢!
GO--if object_id('表A') is not null
-- drop table [表A]
--Go
--Create table [表A]([a] nvarchar(1),[b] nvarchar(2))
--Insert into [表A]
--Select N'a',N'a1'
--Union all Select N'a',N'a2'
--Union all Select N'a',N'a3'
--Union all Select N'a',N'a4'
--Union all Select N'a',N'a5'
--Union all Select N'a',N'a6'
--Union all Select N'b',N'b1'
--Union all Select N'b',N'b2'
--Union all Select N'b',N'b3'
--Union all Select N'b',N'b4'
--Union all Select N'b',N'b5'
--Union all Select N'b',N'b6'
select a,stuff((select ','+b from 表A where a=a.a order by b for xml path('')),1,1,'') as b
from 表A as a
group by a/*
a b
---- ------------------
a a1,a2,a3,a4,a5,a6
b b1,b2,b3,b4,b5,b6
*/
SELECT [a],stuff((select ','+[b] from #1 where [a]=t.[a] for xml path('')),1,1,'') FROM #1 as t
group by [a]