SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Recursive algorithm for category


Recursive algorithm for category

Author
Message
Dung Dinh
Dung Dinh
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 1233
Hi all,
I have a table Category(CategoryID int,ParentCategoryID int, Name nvarchar(1000)).

CategoryID ParentCategoryID Name
1 NULL A
2 1 B
3 1 C
4 2 D
5 2 E
6 3 F
7 1 G
8 5 H
9 7 I
10 8 J



Now I would like to retrieve data as same as

CategoryID Name NameNavigation
1 A NULL
2 B A -> B
3 C A -> C
4 D A -> B -> D
5 E A -> B -> E
6 F A -> C -> F
7 G A -> G
8 H A -> B -> E -> H
9 I A -> G -> I
10 J A -> B -> E -> H -> J


Please help me with any suggestion. I need a generic algorithm to process without limiting depth (level) of category.
Thanks,
Jason-299789
Jason-299789
SSCertifiable
SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)SSCertifiable (5.1K reputation)

Group: General Forum Members
Points: 5115 Visits: 3232
You might want to look at this thread as its very similar to what you want to do,

http://www.sqlservercentral.com/Forums/Topic1379719-391-1.aspx

The only difference is the source table and column names and the format of the output string in the CTE, so you should be able to recode this to suite your requirements.

_________________________________________________________________________
SSC Guide to Posting and Best Practices
Stewart "Arturius" Campbell
Stewart "Arturius" Campbell
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17423 Visits: 7422
Have you tried using a recursive CTE
e.g.
;with CategoryDrillDown AS
(select CategoryID, ParentCategoryID, Name, convert(nvarchar(1000),NULL) AS ParentName
from Category
where ParentCategoryID is null
union all
select Category.CategoryID, Category.ParentCategoryID, Category.Name, CategoryDrillDown.Name AS parentName
from Category
join CategoryDrillDown on CategoryDrillDown.CategoryID = Category.ParentCategoryID)
select * from CategoryDrillDown



____________________________________________
Space, the final frontier? not any more...
All limits henceforth are self-imposed.
“libera tute vulgaris ex”
Dung Dinh
Dung Dinh
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 1233
Jason-299789 (11/7/2012)
You might want to look at this thread as its very similar to what you want to do,

http://www.sqlservercentral.com/Forums/Topic1379719-391-1.aspx

The only difference is the source table and column names and the format of the output string in the CTE, so you should be able to recode this to suite your requirements.


I got the idea to do my situation.
Thanks so much,
Mark Cowne
Mark Cowne
SSCertifiable
SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)SSCertifiable (6.6K reputation)

Group: General Forum Members
Points: 6611 Visits: 25605


DECLARE @Category TABLE(CategoryID int,ParentCategoryID int, Name nvarchar(1000))
INSERT INTO @Category(CategoryID,ParentCategoryID,Name)
VALUES
(1 , NULL, 'A' ),
(2 , 1 , 'B' ),
(3 , 1 , 'C' ),
(4 , 2 , 'D' ),
(5 , 2 , 'E' ),
(6 , 3 , 'F' ),
(7 , 1 , 'G' ),
(8 , 5 , 'H' ),
(9 , 7 , 'I' ),
(10, 8 , 'J' );

WITH Recur AS (
SELECT CategoryID, ParentCategoryID, Name, Name AS NameNavigation
FROM @Category

UNION ALL

SELECT r.CategoryID, c.ParentCategoryID, r.Name, CAST(c.Name + N' -> ' + r.NameNavigation AS nvarchar(1000))
FROM @Category c
INNER JOIN Recur r ON r.ParentCategoryID = c.CategoryID
)
SELECT CategoryID,Name,NameNavigation
FROM Recur
WHERE ParentCategoryID IS NULL
ORDER BY CategoryID;



____________________________________________________

Deja View - The strange feeling that somewhere, sometime you've optimised this query before

How to get the best help on a forum

http://www.sqlservercentral.com/articles/Best+Practices/61537




Dung Dinh
Dung Dinh
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 1233
Mark-101232 (11/7/2012)


DECLARE @Category TABLE(CategoryID int,ParentCategoryID int, Name nvarchar(1000))
INSERT INTO @Category(CategoryID,ParentCategoryID,Name)
VALUES
(1 , NULL, 'A' ),
(2 , 1 , 'B' ),
(3 , 1 , 'C' ),
(4 , 2 , 'D' ),
(5 , 2 , 'E' ),
(6 , 3 , 'F' ),
(7 , 1 , 'G' ),
(8 , 5 , 'H' ),
(9 , 7 , 'I' ),
(10, 8 , 'J' );

WITH Recur AS (
SELECT CategoryID, ParentCategoryID, Name, Name AS NameNavigation
FROM @Category

UNION ALL

SELECT r.CategoryID, c.ParentCategoryID, r.Name, CAST(c.Name + N' -> ' + r.NameNavigation AS nvarchar(1000))
FROM @Category c
INNER JOIN Recur r ON r.ParentCategoryID = c.CategoryID
)
SELECT CategoryID,Name,NameNavigation
FROM Recur
WHERE ParentCategoryID IS NULL
ORDER BY CategoryID;



Yes, I got it :-)
Dung Dinh
Dung Dinh
Say Hey Kid
Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)Say Hey Kid (671 reputation)

Group: General Forum Members
Points: 671 Visits: 1233
By the way, do we have any solution to improve performance of CTE in case the category tree has depth > 4?
As I knew, if we use CTE to do recursive algorithm, SQL engine must read so many times. In my case, there are ~4000 categories and max of depth = 4, and I run CTE

Table 'Category'. Scan count 2, logical reads 2543467, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Worktable'. Scan count 2, logical reads 27561, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Thanks,
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search