假设我有一个临时表,如下所示:
+----+------+
| Id | Value|
+----+------+
| 1 | 1 |
| 1 | 2 |
| 1 | 3 |
| 2 | 1 |
| 2 | 2 |
+----+------+
我希望我的桌子像这样:
+----+----------+
| Id | ValueList|
+----+----------+
| 1 | 1,2,3 |
| 2 | 1,2 |
+----+----------+
所以基本上我需要将我的值分组为逗号分隔列表。 我已经尝试了以下内容:
SELECT Id, STUFF((SELECT ',' + CAST(VALUE AS varchar) FROM @MyTable FOR XML PATH('')), 1 ,1, '') AS ValueList
FROM @MyTable
GROUP BY Id
但我得到的结论是:
+----+---------------------+
| Id | ValueList |
+----+---------------------+
| 1 | 1,1,1,1,1,1,... |
+----+---------------------+
我无法找到我做错的事。有人可以帮助这个查询吗?还是指向正确的方向? 谢谢。
答案 0 :(得分:11)
您缺少子查询中的条件。
SELECT t2.Id, STUFF((SELECT ',' + CAST(VALUE AS varchar) FROM @MyTable t1 where t1.Id =t2.ID FOR XML PATH('')), 1 ,1, '') AS ValueList
FROM @MyTable t2
GROUP BY t2.Id
答案 1 :(得分:5)
在GROUP BY
上使用Id
的一种替代方法是使用select distinct:
SELECT DISTINCT
Id,
STUFF((SELECT ',' + CAST(t2.VALUE AS varchar)
FROM @MyTable t2
WHERE t2.Id = t1.Id
FOR XML PATH('')), 1 ,1, '') AS ValueList
FROM @MyTable t1
答案 2 :(得分:2)
试试这个:
create table #t(id int, value int)
insert into #t values
(1,1),
(1,2),
(1,3),
(2,1),
(2,2)
SELECT t2.Id,
STUFF((SELECT ',' + CAST(VALUE AS varchar) FROM #t t1 where t1.Id =t2.ID FOR XML PATH('')), 1 ,1, '') AS list
FROM #t t2
GROUP BY t2.Id
输出:
Id list
--- -------
1 1,2,3
2 1,2
答案 3 :(得分:1)
也可以将FOR XML与CROSS APPLY(或OUTER APPLY)结合使用。
示例摘录
declare @T table (id int, value int);
insert into @T values (1,1),(1,2),(1,3),(2,1),(2,2);
select id, stuff(x.list,1,1,'') as list
from (select distinct id from @T) as t
cross apply (
select concat(',',t2.value)
from @T t2
where t2.id = t.id
for xml path('')
) x(list)
order by id;
<强>结果:强>
id list
-- -----
1 1,2,3
2 1,2
从MS Sql Server 2017开始,可以使用STRING_AGG代替。
select id, string_agg(value,',') as list
from Yourtable t
group by id;