| Author |
Topic |
|
Access
Starting Member
44 Posts |
Posted - 2007-03-29 : 15:18:50
|
| Hello ,I have a table WORDS.Word_id | word_name | word synonym1 |oval | egg-shaped2 | oval | rounded3 | voyage | travel4 | voyage | journey5 | voyage | tripIn the output I’m looking to have two fields word_name and word_synonym, but values in word_synonym field will be comma delimited.world_name | world synonymoval | egg-shaped, roundedvoyage | travel, journey, tripThank you |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Access
Starting Member
44 Posts |
|
|
tadin
Yak Posting Veteran
63 Posts |
Posted - 2007-03-29 : 16:40:57
|
| I think your table is in 0th Normal Form. Your table is in a master-detail relationship and you need to split it, as far as i know,it's in a denormalized form.My approach would be to..copy the table into a temp table and than get the output. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-29 : 17:06:28
|
| [code]-- Prepare sample dataDECLARE @Words TABLE (WordID INT, WordName VARCHAR(6), WordSynonym VARCHAR(10))INSERT @WordsSELECT 1, 'oval', 'egg-shaped' union allSELECT 2, 'oval', 'rounded' union allSELECT 3, 'voyage', 'travel' union allSELECT 4, 'voyage', 'journey' union allSELECT 5, 'voyage', 'trip'-- Show the expected outputSELECT DISTINCT w1.WordName, STUFF((SELECT TOP 100 PERCENT ',' + w2.WordSynonym FROM @Words AS w2 WHERE w2.WordName = w1.WordName ORDER BY WordID FOR XML PATH('')), 1, 1, '') AS WordSynonymsFROM @Words AS w1ORDER BY 1[/code]Peter LarssonHelsingborg, Sweden |
 |
|
|
Access
Starting Member
44 Posts |
Posted - 2007-03-30 : 19:37:08
|
quote: Originally posted by Peso
-- Prepare sample dataDECLARE @Words TABLE (WordID INT, WordName VARCHAR(6), WordSynonym VARCHAR(10))INSERT @WordsSELECT 1, 'oval', 'egg-shaped' union allSELECT 2, 'oval', 'rounded' union allSELECT 3, 'voyage', 'travel' union allSELECT 4, 'voyage', 'journey' union allSELECT 5, 'voyage', 'trip'-- Show the expected outputSELECT DISTINCT w1.WordName, STUFF((SELECT TOP 100 PERCENT ',' + w2.WordSynonym FROM @Words AS w2 WHERE w2.WordName = w1.WordName ORDER BY WordID FOR XML PATH('')), 1, 1, '') AS WordSynonymsFROM @Words AS w1ORDER BY 1Peter LarssonHelsingborg, Sweden
I gave it a try, but it erroring out.Thank you |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-03-31 : 01:21:50
|
| Do you have SQL Server 2000? Not SQL Server 2005?Peter LarssonHelsingborg, Sweden |
 |
|
|
Access
Starting Member
44 Posts |
Posted - 2007-03-31 : 16:04:40
|
quote: Originally posted by Peso Do you have SQL Server 2000? Not SQL Server 2005?Peter LarssonHelsingborg, Sweden
No, Jsut SQL Server 2005 |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-01 : 03:36:09
|
| If you have SQL Server 2005, the code above works.I am using Developer Edition of SQL Server 2005 and it works great.Which is the error you get?Peter LarssonHelsingborg, Sweden |
 |
|
|
|