SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Alternative ways for Comma separated grouping


Alternative ways for Comma separated grouping

Author
Message
vignesh.ms
vignesh.ms
SSC Eights!
SSC Eights! (848 reputation)SSC Eights! (848 reputation)SSC Eights! (848 reputation)SSC Eights! (848 reputation)SSC Eights! (848 reputation)SSC Eights! (848 reputation)SSC Eights! (848 reputation)SSC Eights! (848 reputation)

Group: General Forum Members
Points: 848 Visits: 516

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 ???
J Livingston SQL
J Livingston SQL
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12625 Visits: 37680
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

Brandie Tarvin
Brandie Tarvin
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39442 Visits: 9291
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 AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65166 Visits: 17979
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 Modens 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)
J Livingston SQL
J Livingston SQL
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12625 Visits: 37680
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 :-D
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

Sean Lange
Sean Lange
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: General Forum Members
Points: 65166 Visits: 17979
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 :-D
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 Modens 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)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search