Discussion:
Convert row data to column
(too old to reply)
Manoj Kumar
2013-05-11 07:32:05 UTC
Permalink
Hi Everyone,

I have a table as follows

S. No. Month
1 Jan
1 Feb
1 Mar
2 Apr
3 Jun
2 Jul
3 Aug

I want the result as follows:

S. No. Month
1 Jan, Feb, Mar
2 Apr, Jul
3 Jun, Aug

The s. No. should contain unique values after concatenation of month.

Please help on this.

Regards,
Manoj
Erland Sommarskog
2013-05-11 08:45:05 UTC
Permalink
Post by Manoj Kumar
I have a table as follows
S. No. Month
1 Jan
1 Feb
1 Mar
2 Apr
3 Jun
2 Jul
3 Aug
S. No. Month
1 Jan, Feb, Mar
2 Apr, Jul
3 Jun, Aug
The s. No. should contain unique values after concatenation of month.
The standard solution is nothing you find by just reading the table
of contents in the T-SQL reference:

SELECT a.S, substring(b.list, 1, len(b.list) - 1)
FROM (SELECT DISTINCT S FROM tbl) AS a
CROSS APPLY (SELECT b.Month + ', '
FROM tbl b
WHERE a.S = b.S
ORDER BY b.Month
FOR XML PATH('')) AS b
--
Erland Sommarskog, Stockholm, ***@sommarskog.se
Manoj Kumar
2013-05-11 12:00:57 UTC
Permalink
Post by Manoj Kumar
Hi Everyone,
I have a table as follows
S. No. Month
1 Jan
1 Feb
1 Mar
2 Apr
3 Jun
2 Jul
3 Aug
S. No. Month
1 Jan, Feb, Mar
2 Apr, Jul
3 Jun, Aug
The s. No. should contain unique values after concatenation of month.
Please help on this.
Regards,
Manoj
Hi I am getting error on list..
Manoj Kumar
2013-05-11 12:08:09 UTC
Permalink
Post by Manoj Kumar
Post by Manoj Kumar
Hi Everyone,
I have a table as follows
S. No. Month
1 Jan
1 Feb
1 Mar
2 Apr
3 Jun
2 Jul
3 Aug
S. No. Month
1 Jan, Feb, Mar
2 Apr, Jul
3 Jun, Aug
The s. No. should contain unique values after concatenation of month.
Please help on this.
Regards,
Manoj
Hi I am getting error on list..
thanks I got it.......

Continue reading on narkive:
Loading...