| Author |
Topic |
|
arorarahul.0688
Posting Yak Master
125 Posts |
Posted - 2008-11-27 : 01:44:22
|
| hi i have a following pattern as inputCol1 Col2A YB YC YD NI need Output asA,B,C YD NHope its not tough enough for you guys so plz help me in this. Thanks For the efforts.Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE |
|
|
snSQL
Master Smack Fu Yak Hacker
1837 Posts |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-11-27 : 02:13:31
|
| DECLARE @table table (Col1 char(2), Col2 char(2))INSERT INTO @table select 'A', 'Y' UNION ALL SELECT 'B', 'Y' UNION ALL select 'C', 'Y' UNION ALL SELECT 'D', 'N'SELECT DISTINCT STUFF((SELECT DISTINCT ',' + CAST(col1 AS VARCHAR(255)) FROM @table WHERE col2 = t.col2 FOR XML PATH('')), 1, 1, ''), col2 FROM @table t |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-11-27 : 02:26:56
|
quote: Originally posted by bklr DECLARE @table table (Col1 char(2), Col2 char(2))INSERT INTO @table select 'A', 'Y' UNION ALL SELECT 'B', 'Y' UNION ALL select 'C', 'Y' UNION ALL SELECT 'D', 'N'SELECT DISTINCT STUFF((SELECT DISTINCT ',' + CAST(col1 AS VARCHAR(255)) FROM @table WHERE col2 = t.col2 FOR XML PATH('')), 1, 1, ''), col2 FROM @table t
This will work from SQL Server 2005 version onwardsMadhivananFailing to plan is Planning to fail |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-11-27 : 03:14:55
|
quote: Originally posted by arorarahul.0688 hi i have a following pattern as inputCol1 Col2A YB YC YD NI need Output asA,B,C YD NHope its not tough enough for you guys so plz help me in this. Thanks For the efforts.Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE
See once output the output is same |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2008-11-27 : 03:16:06
|
quote: Originally posted by bklr [quote]Originally posted by arorarahul.0688 hi i have a following pattern as inputCol1 Col2A YB YC YD NI need Output asA,B,C YD NHope its not tough enough for you guys so plz help me in this. Thanks For the efforts.Rahul Arora 07 BatchNCCE Israna, ######################IMPOSSIBLE = I+M+POSSIBLE
See once output the output is same for this querySELECT DISTINCT STUFF((SELECT DISTINCT ',' + CAST(col1 AS VARCHAR(255)) FROM @table WHERE col2 = t.col2 FOR XML PATH('')), 1, 1, '')as col1, col2 FROM @table t |
 |
|
|
Jai Krishna
Constraint Violating Yak Guru
333 Posts |
Posted - 2008-11-27 : 03:29:58
|
| SELECT DISTINCTSTUFF((SELECT ','+ col1 FROM #table WHERE col2=t.col2 FOR XML PATH('')),1,1,''),t.col2from #table tJai Krishna |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2008-11-27 : 03:37:02
|
| select distinct stuff((select distinct ','+ col1 from ##table t where t.col2=t1.col2 for xml path('')),1,1,'')+' '+t1.col2 from ##table t1I Struggle For Excellence |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-11-27 : 03:40:12
|
| will now each of people who see this change variable names and post as new suggestion? |
 |
|
|
|