In my database there is a table with values below
In need a qry to get the result like this
In my database there is a table with values below
In need a qry to get the result like this
On
Use XML query as below;
DECLARE @tblQuestion AS Table
(
SID INT,
Value VARCHAR(50)
)
INSERT INTO @tblQuestion VALUES(1,'stu')
INSERT INTO @tblQuestion VALUES(1,'vtu')
INSERT INTO @tblQuestion VALUES(1,'ztu')
INSERT INTO @tblQuestion VALUES(2,'stu')
INSERT INTO @tblQuestion VALUES(2,'vtu')
select distinct t.SID,
STUFF((SELECT distinct ', ' + t1.Value
from @tblQuestion t1
where t.SID = t1.SID
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,2,'') Value
from @tblQuestion t;
Output:
OUTPUT