在sql中多行与一个逗号分隔的值

mfuanj7w  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(322)

我们想把多列值显示成单列,下面用逗号分隔的值就是我的表数据

下面是我正在尝试的sql查询:

select 
         name,
         STUFF((SELECT '; ' + facilty FROM leads Name FOR XML PATH('')),1,2,'') 
 as facilty,
         address
    from leads

查询结果

现在,当我在sql server中执行查询时,我希望它如下所示:

ubof19bj

ubof19bj1#

使用where子句:

select name , STUFF((SELECT '; ' + facilty 
                     FROM leads  
                     FOR XML PATH('')
                    ),1,2,'') as facilty, address 
from leads
where name is not null;
5jdjgkvh

5jdjgkvh2#

使用 string_agg() :

select max(name), string_agg(facility, ','), max(address)
from leads
group by leadid;

相关问题