XML 路径 ibn SQL 的分组依据

use*_*480 2 sql t-sql for-xml-path

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

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

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]
Run Code Online (Sandbox Code Playgroud)

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

非常感谢任何帮助

Luk*_*zda 6

您需要关联子查询:

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]
Run Code Online (Sandbox Code Playgroud)