【问题标题】:Group by for XML Path ibn SQLSQL 中 XML 路径的分组依据
【发布时间】:2018-03-22 06:09:59
【问题描述】:

我正在尝试将列中的一些值连接到单个字段中。

到目前为止,我有以下代码。

SELECT DISTINCT [customer id]
      ,[customer name]
      ,STUFF( (SELECT ',' + [description] 
                             FROM [Invoicing].[dbo].[CurrentBillMaster] 
                             ORDER BY [description]
                             FOR XML PATH('')),
                            1, 1, '')
      ,[id]
      ,[Section]
      ,[customerpo]
  FROM [Invoicing].[dbo].[CurrentBillMaster]
  GROUP BY [customer id], [customer name], [description],[qty],
           [identifier],[FromDate],[ToDate],[id],[Section],[customerpo]

代码基本上可以工作,但是我想要连接描述,只是为了显示该唯一 [客户 ID] 的描述

非常感谢任何帮助

【问题讨论】:

  • 添加样本数据和预期输出以获得更好的答案

标签: sql tsql for-xml-path


【解决方案1】:

您需要关联子查询:

SELECT  [customer id]
      ,[customer name]
      ,STUFF( (SELECT ',' + [description] 
                             FROM [Invoicing].[dbo].[CurrentBillMaster] t
                             WHERE  t.Customer_id = c.customer_id  -- here
                             ORDER BY [description]
                             FOR XML PATH('')),
                            1, 1, '')
      ,[id]
      ,[Section]
      ,[customerpo]
  FROM [Invoicing].[dbo].[CurrentBillMaster] c
  GROUP BY [customer id], [customer name], [description],[qty],
           [identifier],[FromDate],[ToDate],[id],[Section],[customerpo]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 2015-08-22
    相关资源
    最近更新 更多