Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Need Query

Author  Topic 

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2009-05-20 : 05:22:22
From below table, i need to retrive all data based on Col4. (i.e group by Col4.) Col1 & Co2 be same through out the table. Col3 must be append by comma based on Col4. See the output below by getting an idea.

Original Table

Col1 Col2 Col3 Col4
SAME HISP Arge Alcibar
SAME HISP Colo AlcIbar
SAME HISP Colo AlcIbar
SAME HISP Colo AlmerIa
SAME HISP Span AlmerIa
SAME HISP Arge Amarilla
SAME HISP Peru Anivole
SAME HISP Arge Antero
SAME HISP Colo Antero

The Ouput be like this

Col1 Col2 Col3 Col4
SAME HISP Arge,Colo Alcibar
SAME HISP Colo,Span AlmerIa
SAME HISP Arge Amarilla
SAME HISP Peru Anivole
SAME HISP Arge,Colo Antero

G. Satish

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-05-20 : 05:37:27
use stuff , it will be more useful to u
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-20 : 05:37:52
select col1,col2,stuff((select ','+col3 from tablename where col4 = t.col4 for xml path('')),1,1,'') as col3 , col4
from tablename t
Go to Top of Page

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2009-05-20 : 05:52:02
Thank you. Its really useful

quote:
Originally posted by bklr

select col1,col2,stuff((select ','+col3 from tablename where col4 = t.col4 for xml path('')),1,1,'') as col3 , col4
from tablename t



G. Satish
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-05-20 : 05:52:52
quote:
Originally posted by satish.gorijala

Thank you. Its really useful

quote:
Originally posted by bklr

select col1,col2,stuff((select ','+col3 from tablename where col4 = t.col4 for xml path('')),1,1,'') as col3 , col4
from tablename t



G. Satish



welcome
Go to Top of Page

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2009-05-20 : 06:00:22
When i use this query..i am almost very near to my requirement. but the repeating occurs. See Column3 in below output. The "Colo" is repeating. How to avoid this. I just want "Arge,Colo"

select distinct Column1,Column2,stuff((select ','+Column3 from tablename where Column4 = t.Column4 for xml path('')),1,1,'') as Column3 , Column4
from tablename t Order by Column4

the output i got is
SAME HISP Arge,Colo,Colo Alcibar
SAME HISP Colo,Span AlmerIa

quote:
Originally posted by bklr

select col1,col2,stuff((select ','+col3 from tablename where col4 = t.col4 for xml path('')),1,1,'') as col3 , col4
from tablename t



G. Satish
Go to Top of Page

satish.gorijala
Posting Yak Master

182 Posts

Posted - 2009-05-20 : 06:01:26
ok, Finally i got it. I use distinct in internal query.

select distinct Column1,Column2,stuff((select distinct ','+Column3 from tablename where Column4 = t.Column4 for xml path('')),1,1,'') as Column3 , Column4
from tablename t Order by Column4



quote:
Originally posted by bklr

quote:
Originally posted by satish.gorijala

Thank you. Its really useful

quote:
Originally posted by bklr

select col1,col2,stuff((select ','+col3 from tablename where col4 = t.col4 for xml path('')),1,1,'') as col3 , col4
from tablename t



G. Satish



welcome



G. Satish
Go to Top of Page

Nageswar9
Aged Yak Warrior

600 Posts

Posted - 2009-05-20 : 06:01:35
quote:
Originally posted by satish.gorijala

When i use this query..i am almost very near to my requirement. but the repeating occurs. See Column3 in below output. The "Colo" is repeating. How to avoid this. I just want "Arge,Colo"

select distinct Column1,Column2,stuff((select ','+Column3 from tablename where Column4 = t.Column4 for xml path('')),1,1,'') as Column3 , Column4
from tablename t Order by Column4

the output i got is
SAME HISP Arge,Colo,Colo Alcibar
SAME HISP Colo,Span AlmerIa

quote:
Originally posted by bklr

select col1,col2,stuff((select ','+col3 from tablename where col4 = t.col4 for xml path('')),1,1,'') as col3 , col4
from tablename t



G. Satish




select col1,col2,stuff((select distinct ','+col3 from tablename where col4 = t.col4 for xml path('')),1,1,'') as col3 , col4
from tablename t
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-05-20 : 06:04:59
see http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page
   

- Advertisement -