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.
| Author |
Topic |
|
dougiel
Starting Member
4 Posts |
Posted - 2008-06-20 : 08:44:11
|
| I have a table with multiple rows for a single reference, e.g.Col1 Col21 John1 Mary1 Tom2 Dick2 AnneHow do I create this view:Col1 Col21 John, Mary, Tom2 Dick, Anne |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-20 : 08:49:49
|
| I forget who I stole this code from, probably Viskh and/or Peso - this looks like Peso's coding--, this question is often asked and you can find many threads on it using the search function here.DECLARE @Sample TABLE (ID INT, Code VARCHAR(3))INSERT @SampleSELECT 290780, 'LT' UNION ALLSELECT 290780, 'AY' UNION ALLSELECT 290781, 'ILS' UNION ALLSELECT 290780, 'AY'-- Show the expected outputSELECT DISTINCT s1.ID, STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID ORDER BY ',' + s2.CODE FOR XML PATH('')), 1, 1, '') AS CODESFROM @Sample AS s1ORDER BY s1.IDSELECT DISTINCT s1.ID, STUFF((SELECT TOP 100 PERCENT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID ORDER BY ',' + s2.CODE FOR XML PATH('')), 1, 1, '') AS CODESFROM @Sample AS s1ORDER BY s1.IDSELECT DISTINCT s1.ID, STUFF((SELECT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID FOR XML PATH('') ), 1, 1, '' ) AS CODESFROM @Sample AS s1ORDER BY s1.IDJim |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 09:43:09
|
quote: Originally posted by jimf I forget who I stole this code from, probably Viskh and/or Peso - this looks like Peso's coding--, this question is often asked and you can find many threads on it using the search function here.DECLARE @Sample TABLE (ID INT, Code VARCHAR(3))INSERT @SampleSELECT 290780, 'LT' UNION ALLSELECT 290780, 'AY' UNION ALLSELECT 290781, 'ILS' UNION ALLSELECT 290780, 'AY'-- Show the expected outputSELECT DISTINCT s1.ID, STUFF((SELECT DISTINCT TOP 100 PERCENT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID ORDER BY ',' + s2.CODE FOR XML PATH('')), 1, 1, '') AS CODESFROM @Sample AS s1ORDER BY s1.IDSELECT DISTINCT s1.ID, STUFF((SELECT TOP 100 PERCENT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID ORDER BY ',' + s2.CODE FOR XML PATH('')), 1, 1, '') AS CODESFROM @Sample AS s1ORDER BY s1.IDSELECT DISTINCT s1.ID, STUFF((SELECT ',' + s2.CODE FROM @Sample AS s2 WHERE s2.ID = s1.ID FOR XML PATH('') ), 1, 1, '' ) AS CODESFROM @Sample AS s1ORDER BY s1.IDJim
This is Peso's . I mostly use the CROSS APPLY approach. |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2008-06-20 : 10:08:08
|
That's what I thought. I'm having trouble wrapping my head around cross apply, so I went with Peso's. I've stolen quite a bit of your code too so it's hard to keep track! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-20 : 10:15:48
|
quote: Originally posted by jimf That's what I thought. I'm having trouble wrapping my head around cross apply, so I went with Peso's. I've stolen quite a bit of your code too so it's hard to keep track!
Cool . I've not made them myself either. i also learn the new methods from others. Way to go . |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-06-20 : 18:16:22
|
quote: Originally posted by jimf That's what I thought. I'm having trouble wrapping my head around cross apply, so I went with Peso's. I've stolen quite a bit of your code too so it's hard to keep track!
FYI, http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81254MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|