SQL Server - 将行连接到逗号分隔列表中

时间:2020-12-22 00:20:17

Let's suppose I have a temporary table which looks like this:

假设我有一个临时表,如下所示:

+----+------+
| Id | Value|
+----+------+    
| 1  |   1  |
| 1  |   2  |
| 1  |   3  |
| 2  |   1  |
| 2  |   2  |
+----+------+

And I want my table to be like this:

我希望我的桌子像这样:

    +----+----------+
    | Id | ValueList|
    +----+----------+    
    | 1  |   1,2,3  |
    | 2  |   1,2    | 
    +----+----------+

So basically I need to group my values as a comma separated list. I already tried the following:

所以基本上我需要将我的值分组为逗号分隔列表。我已经尝试过以下方法:

SELECT Id, STUFF((SELECT ',' + CAST(VALUE AS varchar) FROM @MyTable FOR XML PATH('')), 1 ,1, '') AS ValueList
FROM @MyTable
GROUP BY Id

But I get something like:

但我得到类似的东西:

        +----+---------------------+
        | Id |      ValueList      |
        +----+---------------------+    
        | 1  |   1,1,1,1,1,1,...   |
        +----+---------------------+

I cant find what I am doing wrong. Could someone help with this query? Or point me to a right direction? Thank you.

我找不到我做错了什么。有人可以帮助这个查询吗?还是指向一个正确的方向?谢谢。

4 个解决方案

#1


9  

You are missing the condition inside the sub query.

您缺少子查询中的条件。

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

Demo

演示

#2


5  

One alternative to using GROUP BY on the Id would be to use select distinct:

在Id上使用GROUP BY的一种替代方法是使用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

Demo

#3


2  

Try this :

尝试这个 :

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

output :

输出:

Id          list
---        -------
1           1,2,3
2           1,2

#4


0  

One can also combine a FOR XML with a CROSS APPLY (or an OUTER APPLY) for this.

也可以将FOR XML与CROSS APPLY(或OUTER APPLY)组合使用。

Example snippet:

示例代码段:

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;

Result:

结果:

id  list
--  -----
1   1,2,3
2   1,2

And starting from MS Sql Server 2017, STRING_AGG can be used instead.

从MS Sql Server 2017开始,可以使用STRING_AGG代替。

select id, string_agg(value,',') as list
from Yourtable t
group by id;

#1


9  

You are missing the condition inside the sub query.

您缺少子查询中的条件。

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

Demo

演示

#2


5  

One alternative to using GROUP BY on the Id would be to use select distinct:

在Id上使用GROUP BY的一种替代方法是使用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

Demo

#3


2  

Try this :

尝试这个 :

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

output :

输出:

Id          list
---        -------
1           1,2,3
2           1,2

#4


0  

One can also combine a FOR XML with a CROSS APPLY (or an OUTER APPLY) for this.

也可以将FOR XML与CROSS APPLY(或OUTER APPLY)组合使用。

Example snippet:

示例代码段:

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;

Result:

结果:

id  list
--  -----
1   1,2,3
2   1,2

And starting from MS Sql Server 2017, STRING_AGG can be used instead.

从MS Sql Server 2017开始,可以使用STRING_AGG代替。

select id, string_agg(value,',') as list
from Yourtable t
group by id;