Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Variation on comma-separated strings in T-SQL

Posted on 2013-05-10
5
Medium Priority
?
168 Views
Last Modified: 2013-05-16
I have a SELECT statement which yields data in the following pattern:

Category      Item
------------  -------
Red            A
Red            B
Red            C
Blue           A
Blue           D
Blue           E
Blue           F

Open in new window


I need to instead produce this:

Category      Item
------------  -------
Red           A,B,C
Blue          A,D,E,F

Open in new window


Thanks!
0
Comment
Question by:wlevy
[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
  • 4
5 Comments
 
LVL 16

Assisted Solution

by:Surendra Nath
Surendra Nath earned 500 total points
ID: 39157569
try this

select Y2.Category,(select stuff((select ',' + Item from <yourTable> Y1 where Y1.Category = Y2.Category for xml path('')),1,1,'')
from <yourTable> Y2
group by Y2.Category

Open in new window

0
 

Author Comment

by:wlevy
ID: 39158305
I get an error: The multi-part identifier "Y2.Category" could not be bound. This is on "Y2.Category" immediately following "select"  on line 1.
0
 

Accepted Solution

by:
wlevy earned 0 total points
ID: 39158725
I got it now:

select Y2.Category, stuff((select ',' + Item from <yourTable> Y1 where Y1.Category = Y2.Category for xml path('')),1,1,'')
from <yourTable> Y2

Thanks for pointing me in the right direction,
0
 

Author Comment

by:wlevy
ID: 39158726
Oops, my solution was missing DISTINCT. Should be...

SELECT DISTINCT B.FunctionName, STUFF((SELECT ',' + A.WBSID FROM @table A WHERE A.FunctionName = B.FunctionName FOR XML PATH('')),1,1,'')
FROM @table B
0
 

Author Closing Comment

by:wlevy
ID: 39170734
Neo_Jarvis had the right idea but obviously didn't test his solution, which was buggy.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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