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 |
|
krisis
Starting Member
6 Posts |
Posted - 2005-04-04 : 22:05:46
|
| I have a product_group table as:group_id, parent_id, name1, 0, Software2, 0, Hardware3, 1, Operating Systems4, 1, Productivity5, 1, Games6, 2, Videocards7, 2, MotherboardsI wish to be able to select the full path of group names. i.e. for group_id 6 to be able to grab:Hardware, VideocardsHow can I do this in a single SQL query? |
|
|
rfrancisco
Yak Posting Veteran
95 Posts |
Posted - 2005-04-04 : 22:22:09
|
| Try this:SELECT isnull(B.name + ',', '') + A.nameFROM product_group A LEFT OUTER JOIN product_group BON A.parent_id = B.group_idWHERE A.group_id = 6This will only work for a single-level hierarchy. If you have more than one level, that's a different story. |
 |
|
|
derrickleggett
Pointy Haired Yak DBA
4184 Posts |
Posted - 2005-04-04 : 23:22:12
|
| http://www.sqlteam.com/item.asp?ItemID=8866MeanOldDBAderrickleggett@hotmail.comWhen life gives you a lemon, fire the DBA. |
 |
|
|
krisis
Starting Member
6 Posts |
Posted - 2005-04-04 : 23:39:01
|
| Thanks. Interesting article.. however, it is only that single-level hierarchy, so the first query answer works great for now. |
 |
|
|
|
|
|
|
|