【问题标题】:MS SQL Concatenate multiple values into a single column [duplicate]MS SQL将多个值连接到一列[重复]
【发布时间】:2018-07-06 11:24:38
【问题描述】:

我目前有下表;

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

任何帮助将不胜感激!

【问题讨论】:

  • 标记您正在使用的 DBMS。这个问题的答案取决于它。
  • 您使用的是哪个数据库?
  • 我希望您不打算将分组值保存在列中,对吧?
  • 投票结束,因为不清楚您的要求,因为不了解您的数据库,我们无法帮助您。但是,无论如何,您的问题可能与其他几个问题重复。

标签: sql sql-server string-aggregation


【解决方案1】:

根据您给定的详细信息,假设下面有一个表#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  

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

【讨论】:

    猜你喜欢
    • 2013-04-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 2019-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多