Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Alternative ways for Comma separated grouping Expand / Collapse
Author
Message
Posted Friday, April 18, 2014 3:44 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: 2 days ago @ 3:38 AM
Points: 141, Visits: 398
create table #mytable (id int identity,[group] varchar(100),decision varchar(100),category varchar(100))

Insert into #mytable values ('1111','yes','A')
Insert into #mytable values ('1111','yes','B')
Insert into #mytable values ('2222','no','A')
Insert into #mytable values ('3333','yes','A')
Insert into #mytable values ('3333','yes','C')
Insert into #mytable values ('4444','no','A')
Insert into #mytable values ('4444','no','B')

Output Query :

SELECT categories, COUNT(*) count
FROM (
SELECT
STUFF((SELECT ',' + mt.category
FROM mytable mt
WHERE m.[group] = mt.[group]
ORDER BY mt.category
FOR XML PATH(''), TYPE).
value('.', 'NVARCHAR(MAX)'), 1, 1, '') categories
FROM mytable m
GROUP BY [group]
) z
GROUP BY categories;


I got this code somewhere from internet & I like to know alternative ways to do this ... Is there any other better way ??

also explain how does above query works ???
Post #1562952
Posted Friday, April 18, 2014 4:20 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 1,912, Visits: 19,453
quick Google....heres an article that may help

http://davidduffett.net/post/5334646215/get-a-comma-separated-list-of-values-in-sql-with-for


______________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Post #1562957
Posted Friday, April 18, 2014 5:37 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 8:54 AM
Points: 5,579, Visits: 6,363
vignesh.ms (4/18/2014)
also explain how does above query works ???


SQL Server Books Online is your best friend. If you don't have a local copy, download it from Microsoft.

Click Index and search XML [SQL Server]. If you have questions on the BOL material, post them here and we'll do our best to answer them.


Brandie Tarvin, MCITP Database Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #1562975
Posted Friday, April 18, 2014 7:33 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
J Livingston SQL (4/18/2014)
quick Google....heres an article that may help

http://davidduffett.net/post/5334646215/get-a-comma-separated-list-of-values-in-sql-with-for


JLS, I can't get that link to load. It just spins and spins. From the url is it using FOR XML like the example already posted here or is there another approach being used. I am curious because using FOR XML to generate a comma separated list is the easiest and fastest way I know of.


_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1563025
Posted Friday, April 18, 2014 8:16 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 3:19 AM
Points: 1,912, Visits: 19,453
Sean Lange (4/18/2014)
J Livingston SQL (4/18/2014)
quick Google....heres an article that may help

http://davidduffett.net/post/5334646215/get-a-comma-separated-list-of-values-in-sql-with-for


JLS, I can't get that link to load. It just spins and spins. From the url is it using FOR XML like the example already posted here or is there another approach being used. I am curious because using FOR XML to generate a comma separated list is the easiest and fastest way I know of.



odd...worked earlier...all I get now is spins as well ???...all it was an explanation of how XML path works in easy steps.....which the OP could find via googlefoo
It wasn't a better method.


______________________________________________________________
you can lead a user to data....but you cannot make them think
and remember....every day is a school day
Post #1563043
Posted Friday, April 18, 2014 8:22 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:13 PM
Points: 12,995, Visits: 12,414
J Livingston SQL (4/18/2014)
Sean Lange (4/18/2014)
J Livingston SQL (4/18/2014)
quick Google....heres an article that may help

http://davidduffett.net/post/5334646215/get-a-comma-separated-list-of-values-in-sql-with-for


JLS, I can't get that link to load. It just spins and spins. From the url is it using FOR XML like the example already posted here or is there another approach being used. I am curious because using FOR XML to generate a comma separated list is the easiest and fastest way I know of.



odd...worked earlier...all I get now is spins as well ???...all it was an explanation of how XML path works in easy steps.....which the OP could find via googlefoo
It wasn't a better method.


Ahh gotcha.



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1563046
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse