I had the following table structure which is listed below
Budhol COCODE BEN OBJ SPARE2 SPARE1 TASKNO Value Field Code
---------------------------------------------------------------------------------
362103 36 362101 991003 NULL MA1001 NULL 4516 613030 001
362104 36 362104 991003 NULL MA1001 NULL 9088 613030 002
362103 36 362101 991003 NULL MA1001 NULL 3387 613030 003
362103 36 362101 991003 NULL MA1001 NULL 4026 613030 004
This is the required output
Budhol COCODE BEN OBJ SPARE2 SPARE1 TASKNO Value Field Code
---------------------------------------------------------------------------------
362103 36 362101 991003 NULL MA1001 NULL 11929 613030 001,003,001
362104 36 362104 991003 NULL MA1001 NULL 9088 613030 002
i.e By doing group by i need sum of all values and comma separated Codes
I had tried below query but output is not matching im getting all the codes for all the columns
SELECT
B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2,
SUM(B.value) AS Value ,
Code = STUFF((SELECT ', ' + Code
FROM #temp2 b
WHERE b.BEN = B.BEN
AND b.Budhol = B.Budhol
AND b.COCODE = B.COCODE
AND b.FIELD = B.FIELD
AND b.SPARE1 = B.SPARE1
AND b.SPARE2 = B.SPARE2
FOR XML PATH('')), 1, 1, '')
FROM
#temp2 B
GROUP BY
B.BEN, B.Budhol, B.COCODE, B.FIELD, B.OBJ, B.SPARE1, B.SPARE2;