Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

concatenate group by, pls help 1

Status
Not open for further replies.

peac3

Technical User
Jan 17, 2009
226
AU
Hi guys,

I have data as per below that I need to concatenate payment based on paymnt_group id.

Code:
paymt_id  paymt_desc    paymt_group_id
1          Electricity       1
2          Grocery           1
3          Gas               1
4          Credit Card       2
5          Home loan         2

The expected result would be:

Code:
paymt_group_desc             paymnt_group_id
Electricity, Grocery, Gas          1
Credit Card, Home Loan             2

Your soon answer will be appreciated guys.
Thanks.
 
the payment group desc should have limit up to 100 characters
 
Hi,

Try something like this:

Code:
SELECT DISTINCT
    a.paymnt_group_id, 
    STUFF( (SELECT ', ' + s.paymnt_desc
            FROM MyTable as s
            WHERE s.paymnt_group_id = a.paymnt_group_id
            FOR XML PATH(''), TYPE).value('.', 'varchar(100)')
           ,1, 2, '')
                                 
FROM MyTable as a

Hope this helps.

[URL unfurl="true"]http://www.imoveisemexposicao.com.br/imobiliarias-em-guarulhos[/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top