I got so many solutions to merge values by comma. I have some tricky requirement of merging ids by comma. I have around 10 columns in a table. For eg.
declare @t1 TABLE (id INT, NAME VARCHAR(MAX), address1 varchar(max), mobileno varchar(max));
INSERT @t1 values (1,'100',null,'1111111111');
INSERT @t1 values (2,'100','pune','1111111111');
INSERT @t1 values (3,'200',null,null);
INSERT @t1 values (4,'300','pune',null);
INSERT @t1 values (5,'200',null,'1111111111');
SELECT
STUFF((
SELECT ','+CAST(ID AS VARCHAR(10)) [text()]
FROM @t1 as B
WHERE B.NAME = A.NAME
FOR XML PATH('')),1,1,'') as ID, NAME, address1, mobileno
FROM @t1 as A
GROUP BY NAME, address1, mobileno
For eg.
ID | Value
---+-------
1 | 100
2 | 100
3 | 200
4 | 200
5 | 300
I need output like
ID | Value
---|------
1,2| 100
3,4| 200
5| 300
PLEASE NOTE: The
','+ ID
must make sure ID is inVARCHAR
or similar string type, otherwise need to convert before it works correctly.