Dept_Id E_Id
1 2
1 3
1 4
1 5
我想输出如下:1-2,3,4,5
答案 0 :(得分:0)
试试这个,
declare @t table (Dept_Id char, E_Id char)
insert into @t(Dept_Id , E_Id) values
(1,2),
(1,3),
(1,4),
(1,5),
(2,2)
select Dept_Id + '- '+STUFF((SELECT ',' + p.E_Id
from @t p
where p.Dept_Id=n.Dept_Id
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'') as result
from @t n group by Dept_Id