I am trying to concatenate some values from a column into a single field.
So far I have the below code.
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]
The code largely works, however I want the concatenated description, just to show descriptions for that unique [customer id]
Any help greatly appreciated
You need to correlate subquery:
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]