?
Solved

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

Posted on 2014-07-29
3
Medium Priority
?
5,439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 2000 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 66

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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

801 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