| Author |
Topic |
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-10-13 : 05:00:16
|
Hi, plz provide the query for below reqtThe table is as belowName Dept1 Dept2Syam A,B C,DSyam F D,LSyam M,A YI am looking for the output as belowName ResultSyam A, B, F, M, A, C, D, D, L, YI tried below query. But this is only for one column. how can i appedn second column data to this.Select top 1 Name,stuff((select ','+Dept1 from table where Name = 'Syam' for xml path('')),1,1,'') as Resultfrom Table t where Name = 'Syam'From above query i got result as Syam A, B, F, M, ABut i want output asSyam A, B, F, M, A, C, D, D, L, Ydeveloper :) |
|
|
sanoj_av
Posting Yak Master
118 Posts |
Posted - 2009-10-13 : 05:14:28
|
| create Table #T(Name varchar(100), Dept1 varchar(100), Dept2 varchar(100))Insert into #T Select 'Syam', 'A,B','C,D' union allSelect 'Syam','F','D,L'union allSelect 'Syam','M,A','Y'Select * from #T SELECT Name, LEFT(T.Dept1,LEN(T.Dept1)-1) AS Dept1 FROM ( SELECT DISTINCT Name FROM #T ) T1 CROSS APPLY ( SELECT Dept1 + ','+ Dept2 + ',' FROM #T T2 WHERE T1.Name=T2.Name FOR XML PATH ('') ) T(Dept1) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-10-13 : 07:06:30
|
| try like thisSelect top 1 Name,stuff((select ','+Dept1 + +','+ Dept2 from #T for xml path('')),1,1,'') as Resultfrom #T t where Name = 'Syam'Select top 1 Name,stuff((select ','+Dept1 from #T for xml path('')) + (select ','+ dept2 from #t for xml path('')),1,1,'') as Resultfrom #T t where Name = 'Syam' |
 |
|
|
satish.gorijala
Posting Yak Master
182 Posts |
Posted - 2009-10-13 : 08:52:07
|
Thank you bklr.quote: Originally posted by bklr try like thisSelect top 1 Name,stuff((select ','+Dept1 + +','+ Dept2 from #T for xml path('')),1,1,'') as Resultfrom #T t where Name = 'Syam'Select top 1 Name,stuff((select ','+Dept1 from #T for xml path('')) + (select ','+ dept2 from #t for xml path('')),1,1,'') as Resultfrom #T t where Name = 'Syam'
developer :) |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-10-13 : 09:40:05
|
| welcome |
 |
|
|
|
|
|