have the following table coming from this query:
SELECT
[Document],
[Description],
[Value],
FROM [DocDetails]
Document Description Value Line No_
120 First Row 100 1
120 Second Row 0 2
120 Third row 0 3
120 Fourth row 0 4
120 fifth row 0 5
120 sixth row 203 6
120 seventh row 256 7
120 eighth row 259 8
120 ninth row 0 9
120 tenth row 0 10
120 eleventh row 0 11
I need to concatenate the description according to the value. I would need such result:
Document Description Value
120 First Row;second row;Third row;Fourth row;fifth row 100
120 sixth row 203
120 seventh row 256
120 eighth row;ninth row;tenth row;eleventh row 259
I tried the following:
SELECT
[Document],
All_Descriptions = STUFF(
(SELECT ';' + Description AS [text()]
FROM [DocDetails] D1
WHERE D1.[Document] = D2.[Document]
FOR XML PATH('')),1,1,'')
FROM [DocDetails] D2
GROUP BY D2.[Document]
As I don't have a variable that specifies the order I am not able to concatenate properly ( the code above concatenates everything but that's not what I want). Also if I group by value I am not getting the desired result. How can I tell SQL to basically "concatenate the row with an amount with all the following having value 0"
Thanks for your help!
[Line_No]in the sample data...