June 20, 2017 at 10:47 pm
Hi everyone,
Below is a table example (Table name: Dummy)
Number Value
12345 xyz
12345 abc
12345 zcd
I need an SQL query which will give the result set as
12345 xyz, abc, zcd
Thanks in advance
June 20, 2017 at 11:43 pm
ashok.theagarajan - Tuesday, June 20, 2017 10:47 PMHi everyone,Below is a table example (Table name: Dummy)
Number Value
12345 xyz
12345 abc
12345 zcdI need an SQL query which will give the result set as
12345 xyz, abc, zcdThanks in advance
SELECT
a.Number
, Value = STUFF(( SELECT ',' + CAST(b.Value AS VARCHAR)
FROM Dummy b
WHERE a.Number = b.Number
ORDER BY b.Value
FOR XML PATH(''), TYPE).value('(./text())[1]','VARCHAR(MAX)'
), 1, 1, '')
FROM Dummy a
GROUP BY a.Number;
June 21, 2017 at 12:55 am
Thank you !!!!! Works like a charm
June 21, 2017 at 10:08 am
Or:WITH Dummy (Number, [Value]) AS
(
SELECT
a
, b
FROM
(
VALUES
(12345, 'xyz')
, (12345, 'abc')
, (12345, 'zcd')) t (a, b)
)
SELECT
a.Number
, [xyz]
, [abc]
, [zcd]
FROM Dummy
PIVOT
(Max(Value) FOR [Value] IN ([xyz]
, [abc]
, [zcd])) a;
June 21, 2017 at 10:38 am
Hey ! Thanks, But as you can see it provides the result set in columns instead of it being on a single column with comma separated values.
Also, the earlier method works for as many rows as i have the data for.....
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply