Solved

T-SQL convert rows to comma-separated string, with a GROUP BY

Posted on 2014-07-29
3
4,561 Views
Last Modified: 2016-11-28
Hello my under-apprecitated SQL genius buddies..

I have a two-column SQL 2012 table (disguised to protect the innocent), one of which is a group name, and one is numbers
IF OBJECT_ID('tempdb..test') IS NOT NULL
   DROP TABLE #test
GO

CREATE TABLE #test (
   group_name varchar(10), 
   id int
)

insert into #test (group_name, ID) 
values 
   ('red', 101), ('red', 102), ('blue', 103), ('blue', 104), ('hazel', 105), ('mauve', 106),
   ('grape', 107), ('red', 108), ('blue', 109), ('purple', 110), ('grape', 111), ('red', 112)

Open in new window

I need to generate some T-SQL, using FOR XML PATH I think but am not sure, to group by the group_name column, and return all of the ID's as a comma-separated string, like...

group_name    id_list
red              101, 102, 108, 112
blue            103, 104, 109
hazel          105
mauve        106,
grape          107, 111
purple        110

Thanks in advance.
Jim
0
Comment
Question by:Jim Horn
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 500 total points
ID: 40227603
SELECT DISTINCT group_name,
      STUFF(
      (
            SELECT ',' + CAST(id AS VARCHAR)
            FROM #test AS t2
            WHERE t2.group_name = t.group_name
            FOR XML PATH('')
      ), 1, 1, '') AS id_list
FROM #test AS t
0
 
LVL 65

Author Closing Comment

by:Jim Horn
ID: 40227615
Looks like a winner.  Thanks.
0
 

Expert Comment

by:Ashutosh` Pandey
ID: 41905225
Thanks...Its working Fine
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
T-SQL: Subtracting Amounts from "Among Rows" 3 43
Need to update TableA to TableB 6 34
Managing Columnstore Indexes 2 18
SQL VIEW 7 23
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now