MS SQL 将多个值连接成一个列

MS SQL Concatenate multiple values into a single column

我目前有以下table;

Invoice   Client   Purchase Order
1000      A1       1234
1000      A1       1235
1001      B2       1236
1001      B2       1237
1002      B2       1238

我正在寻找一种快速到达的方式;

Invoice   Client   Purchase Orders
1000      A1       1234 1235
1001      B2       1236 1237
1002      B2       1238

如有任何帮助,我们将不胜感激!

根据您提供的详细信息,假设下面是 table #temp,样本数据:

create table #temp (
     invoice int,
     client varchar(5),
    [purchase order]  int
)

insert into #temp
select 1000,'A1',1234  union all
select 1000,'A1',1235  union all
select 1001,'B2',1236  union all
select 1001,'B2',1237  union all
select 1002,'B2',1238

现在您可以根据需要的输出使用 FOR XML 使用以下查询:

select distinct tp1.invoice,tp1.client,  
(  
    SELECT convert(varchar(10),[purchase order]) + ' ' as [text()]  
    from #temp tp  
    where tp.invoice=tp1.invoice and tp.client=tp1.client  
    for XML path('')  
) as [purchase order]  
from #temp tp1  

如果您有任何疑问,请告诉我。