分组依据 XML 路径 ibn SQL

Group by for XML Path ibn SQL

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

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

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]

的描述

非常感谢任何帮助

您需要关联子查询:

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]