日期:2014-05-17 浏览次数:20638 次
USE test
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
*/