Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert row data to column

7 views
Skip to first unread message

Manoj Kumar

unread,
May 11, 2013, 3:32:05 AM5/11/13
to
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

unread,
May 11, 2013, 4:45:05 AM5/11/13
to
Manoj Kumar (manoj...@gmail.com) writes:
> 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.

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, esq...@sommarskog.se

Manoj Kumar

unread,
May 11, 2013, 8:00:57 AM5/11/13
to
Hi I am getting error on list..

Manoj Kumar

unread,
May 11, 2013, 8:08:09 AM5/11/13
to
thanks I got it.......
0 new messages