Solved

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

Posted on 2014-07-29
3
4,991 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Suggested Solutions

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

737 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