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

Need Query for a problem Expand / Collapse
Author
Message
Posted Sunday, June 16, 2013 8:57 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: 2 days ago @ 11:46 PM
Points: 138, Visits: 384
Hello Everybody,

Consider the following table,


create table SampleTable
(Category varchar(100), Organisams varchar(100) )


Insert into Sampletable values ('Animals','Lion')
Insert into Sampletable values ('Birds','Dove')
Insert into Sampletable values ('Plants','Neem')
Insert into Sampletable values ('Animals','Tiger')
Insert into Sampletable values ('Birds','Eagle')
Insert into Sampletable values ('Plants','Mango Tree')
Insert into Sampletable values ('Animals','Cow')
Insert into Sampletable values ('Birds','Parrot')
Insert into Sampletable values ('Plants','Lime Tree')


I Need following output,

Category | Organisms
-----------------------------
Animals | Lion,Tiger,Cow
Birds | Dove,Eagle,Parrot
Plants | Neem,Mango Tree,Lime Tree

Thanks in Advance
Post #1463959
Posted Sunday, June 16, 2013 9:15 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:37 AM
Points: 2,649, Visits: 4,726
WITH CTE AS
(
SELECT DISTINCT
Category
FROM Sampletable
)
SELECT Category,
Organisams = STUFF((
SELECT ',' + Organisams
FROM Sampletable
WHERE Category = CTE.Category
ORDER BY Organisams
FOR XML PATH(''),
TYPE).value('.','varchar(max)'),1,1,'')
FROM CTE
ORDER BY Category;

There is an article related to this method below
http://www.sqlservercentral.com/articles/comma+separated+list/71700/



Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1463961
Posted Sunday, June 16, 2013 9:24 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: 2 days ago @ 11:46 PM
Points: 138, Visits: 384
hats off to you...

Thanks for the query..

I have derived the output but that was quite complex


create table SampleTable2
(id int identity(1,1) ,Category varchar(100), Organisams varchar(100) )

insert into SampleTable2(Category)
select distinct Category from sampletable


DECLARE @listStr VARCHAR(MAX) ,@Category varchar(100),@i int
set @i =1
while (@i <= (select MAX(id)from SampleTable2 ))
begin
set @Category = (select category from SampleTable2 where id = @i)
SELECT @listStr =''
SELECT @listStr = COALESCE(@listStr,'') +','+Organisams
FROM SampleTable where category=@category

update SampleTable2 set Organisams = right(@listStr,LEN(@listStr)-1)
where Category = @Category
set @i = @i+1
End
Post #1463962
Posted Sunday, June 16, 2013 9:26 AM
SSC-Enthusiastic

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

Group: General Forum Members
Last Login: 2 days ago @ 11:46 PM
Points: 138, Visits: 384
Works fine but im a newbie to SQL ...

Please explain how it works....

Post #1463963
Posted Sunday, June 16, 2013 9:29 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 2:37 AM
Points: 2,649, Visits: 4,726
vignesh.ms (6/16/2013)
Works fine but im a newbie to SQL ...

Please explain how it works....



The explanation is present in the link I had provided earlier. I will add it once again for your reference.
http://www.sqlservercentral.com/articles/comma+separated+list/71700/

There is another article which contains a few more methods to achieve the same result
You can check those as well and use whichever you prefer
https://www.simple-talk.com/sql/t-sql-programming/concatenating-row-values-in-transact-sql/

Edit:Added one more link to an article



Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1463964
Posted Monday, June 17, 2013 10:11 AM
Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Wednesday, July 30, 2014 10:34 AM
Points: 386, Visits: 624
FOR XML is one of the best tools in MSSQL but I would regard it as something that only a power user should attempt as it's syntax is confusing.

Basically it allows you to take an undefined number of records with a related key and convert them into a nested XML Group to pass on to other systems in a structured format (E.g. to be consumed by a webservice)

<BOOKS>
<BOOK>Wizard of Oz </BOOK>
<BOOK>Frankensteins Monster</BOOK>
<BOOK>Moby Dick</BOOK>
</BOOKS>

Building comma separated lists is the same concept except instead of <BOOK>...</BOOK> we will just use ' , '


I would say don't worry about how it works, just play with the different parts to see what effect is has on the output. This will allow you to adapt the example to your real code.
Post #1464248
Posted Monday, June 17, 2013 10:37 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, August 29, 2014 2:32 PM
Points: 13,302, Visits: 12,168
aaron.reese (6/17/2013)

I would say don't worry about how it works, just play with the different parts to see what effect is has on the output. This will allow you to adapt the example to your real code.


I would have the exact opposite answer here. You are asking the OP to just blindly accept the code and move on. This is patently bad advice. It is the OP that has to support this code, they have to answer questions from others about how it works and what it does. An answer to those questions along the lines of "I don't know, I just got this code from some person on the Internet" is not a very good answer.

To the OP, I would read through the articles that Kingston has provided and try to understand how this works. You don't need to know every single detail but you should understand what it is doing and why it works.


_______________________________________________________________

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 #1464276
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse