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 |
|
pigeonrandle
Starting Member
2 Posts |
Posted - 2007-09-13 : 08:02:59
|
| This is my first post, so hello everyone!I hope someone can help as this is bending my brain in unusual directions...I have a table of categories...CategoryID | ParentCategoryID | CategoryNameand a table of productsProductID | ProductName |ProductCategoryIDand i want to return results for all the products in a category, but if that category has sub categories, then i want to return all those products aswell.In other words, if i choose shoes as an example...Shoes.....Stilletos..........Prada...............product1..........Gucci...............product2.....Trainers..........Nike...............product3..........Puma...............product4so if i ask for the contents of shoes i expect products 1,2,3,4and the contents of Stilletos would be products 1,2and Prada would just be product1So i guess i'm asking1) how on earth do i do this?!Cheers in advance,James. |
|
|
ValterBorges
Master Smack Fu Yak Hacker
1429 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 08:13:45
|
With a Common Table Expression, CTE. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 09:25:04
|
[code]DECLARE @Sample TABLE (ID INT, ParentID INT, Name VARCHAR(1000))INSERT @SampleSELECT 7, NULL, 'Ljunggren' UNION ALLSELECT 3, 1, 'Gulli' UNION ALLSELECT 8, 7, 'Kerstin' UNION ALLSELECT 1, NULL, 'Rosberg' UNION ALLSELECT 4, 2, 'Peter' UNION ALLSELECT 5, 3, 'Susanne' UNION ALLSELECT 2, 1, 'Jan-Eric' UNION ALLSELECT 10, 9, 'Jennie' UNION ALLSELECT 6, 3, 'Annelie' UNION ALLSELECT 9, 7, 'Kenneth' UNION ALLSELECT 11, 9, 'Jessica';WITH Yak (ID, ParentID, Name, Path, Indent)AS ( SELECT ID, ParentID, Name, CONVERT(VARCHAR, ROW_NUMBER() OVER (ORDER BY Name)), 0 FROM @Sample WHERE ParentID IS NULL UNION ALL SELECT s.ID, s.ParentID, s.Name, CONVERT(VARCHAR, y.Path + ',' + CONVERT(VARCHAR, ROW_NUMBER() OVER (ORDER BY s.Name DESC))), y.Indent + 1 FROM @Sample AS s INNER JOIN Yak AS y ON y.ID = s.ParentID)SELECT ID, ParentID, REPLICATE('.....', Indent) + Name, PathFROM YakORDER BY Path[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-09-13 : 09:26:41
|
Output from query aboveID ParentID (No column name) Path-- -------- ----------------- -----7 NULL Ljunggren 18 7 .....Kerstin 1,19 7 .....Kenneth 1,211 9 ..........Jessica 1,2,110 9 ..........Jennie 1,2,21 NULL Rosberg 22 1 .....Jan-Eric 2,14 2 ..........Peter 2,1,13 1 .....Gulli 2,25 3 ..........Susanne 2,2,16 3 ..........Annelie 2,2,2 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
pigeonrandle
Starting Member
2 Posts |
Posted - 2007-09-13 : 09:52:14
|
| Err, WOW! thanks for all your suggestions.James. |
 |
|
|
|
|
|
|
|