Reputation: 222
I have below table
TABLE A
| S | D |
----------
| 1 | 01 |
| 2 | 01 |
| 2 | 03 |
I want my query result in the below format
| S | D |
|1,2| 01 |
| 2 | 03 |
How can I get this in SQL
Upvotes: 1
Views: 222
Reputation: 3342
with the help of STUFF
we can achieve this
declare @temp table
(
S int,
D nvarchar(10)
)
insert into @temp values (1,'01')
insert into @temp values (2,'01')
insert into @temp values (2,'03');
SELECT D,
STUFF(
(SELECT ',' + CAST(S AS VARCHAR(10))
FROM @temp
WHERE D = a.D
FOR XML PATH (''))
, 1, 1, '') AS Remark
FROM @temp AS a
GROUP BY D
Upvotes: 2