Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Build menu with CTE .... Rate Topic Display Mode Topic Options
Author
 Message
 Posted Monday, September 23, 2013 3:22 AM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 6:57 PM Points: 3,590, Visits: 5,099
 hunchback (9/22/2013)One way to accomplish this is using a varbinary column to concatenate the sequence of numbers based on the position value.`WITH C1 AS (SELECT Id, IdRoot, Name, Position, CAST(ROW_NUMBER() OVER(ORDER BY position) AS varbinary(900)) AS order_val, 0 AS lvlFROM menu WHERE IdRoot = 0UNION ALLSELECT C.Id, C.IdRoot, C.Name, C.Position, CAST(P.[path] + CAST(ROW_NUMBER() OVER(PARTITION BY P.IdRoot ORDER BY C.Position) AS BINARY(8)) AS varbinary(900)), P.lvl + 1FROM C1 AS p INNER JOIN menu AS C ON p.Id = C.IdRoot)SELECT REPLICATE(SPACE(4), 2 * lvl) + [name] AS menuFROM C1ORDER BY order_val;GO/*menuERP ACC HR PAYMENT PROCESS CANCEL VACATIONS ABSENTS SALES*/`To generate the numbers in specific order we can use a ranking function like ROW_NUMBER, where you can use multiple columns to drive the sort order (by name or position or whatever other combination).You can read more about other uses of he ranking functions in the last book from Itzik Ben-Gan about T-SQL Querying.Inside Microsoft® SQL Server® 2008: T-SQL QueryingMicrosoft SQL Server 2012 High-Performance T-SQL Using Window FunctionsAs Jeff said, NM=Nice Method (?). Except I think that P.[Path] should be p.Order_val in the recursive leg of your CTE. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!
Post #1497303
 Posted Monday, September 23, 2013 3:26 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Tuesday, April 15, 2014 10:49 AM Points: 513, Visits: 1,123
 dwain.c (9/23/2013)As Jeff said, NM=Nice Method (?). Except I think that P.[Path] should be p.Order_val in the recursive leg of your CTE.Yes, had to change it to work but it's just fine... Pedro If you need to work better, try working less...
Post #1497305
 Posted Monday, September 23, 2013 3:29 AM
 Hall of Fame Group: General Forum Members Last Login: Yesterday @ 6:57 PM Points: 3,590, Visits: 5,099
 PiMané (9/23/2013)dwain.c (9/23/2013)As Jeff said, NM=Nice Method (?). Except I think that P.[Path] should be p.Order_val in the recursive leg of your CTE.Yes, had to change it to work but it's just fine... PedroFigured you must've.I looked at this in the morning but didn't have time, so I came back to study it. Any time Jeff approves of something it makes me want to understand.Now I think I do so I'm probably the better for it. My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!
Post #1497306
 Posted Monday, September 23, 2013 5:40 AM
 SSC Journeyman Group: General Forum Members Last Login: Monday, April 14, 2014 12:25 PM Points: 99, Visits: 496
 I changed [path] by order_val in the recursive part and now it should run flawless.A recursive CTE is still an iterative approach so I do not hold my breath hoping for better performance.You can add a clustered index by ID and a nonclustered by IdRoot.
Post #1497334
 Posted Monday, September 23, 2013 5:43 AM
 Mr or Mrs. 500 Group: General Forum Members Last Login: Tuesday, April 15, 2014 10:49 AM Points: 513, Visits: 1,123
 hunchback (9/23/2013)I changed [path] by order_val in the recursive part and now it should run flawless.A recursive CTE is still an iterative approach so I do not hold my breath hoping for better performance.You can add a clustered index by ID and a nonclustered by IdRoot.It's better than the recursive cursor it's currently using...Instead of 7ms and 530reads, for 28 rows, takes 3ms and 321 reads.And for bigger menus the difference is even greater. Pedro If you need to work better, try working less...
Post #1497336
 Posted Monday, September 23, 2013 7:51 AM
 SSC-Dedicated Group: General Forum Members Last Login: Yesterday @ 4:00 PM Points: 35,967, Visits: 30,258
 The "NM" stands for "Never Mind". I had posted a rebuttal saying that the code didn't sort alphabetically, which seemed logical to me at the time. Then I realized that it did sort by the given ID using the same sort method that I had in my first article on hierarchies, compared it to the order the OP was looking for, and realized my mistake. I removed my mistaken post and, since we can't delete posts, just put in a quick "NM".Still, it is a nice method... especially since I've used the same method in the past. --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1497391
 Posted Monday, September 23, 2013 7:56 AM
 SSC-Dedicated Group: General Forum Members Last Login: Yesterday @ 4:00 PM Points: 35,967, Visits: 30,258
 hunchback (9/23/2013)I changed [path] by order_val in the recursive part and now it should run flawless.A recursive CTE is still an iterative approach so I do not hold my breath hoping for better performance.You can add a clustered index by ID and a nonclustered by IdRoot.Well said. Oddly enough, a well constructed WHILE loop that iterates over the Levels of the menu in a manner similar to the rCTE would probably do as well. In either case, the recursion is necessary and isn't as bad as an rCTE that counts because the rCTE method that we both used (and a similar WHILE loop would use) processes an entire set (all nodes at a given level) instead of just individual rows. --Jeff Moden"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column." "Change is inevitable. Change for the better is not." -- 04 August 2013(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013Helpful Links:How to post code problemsHow to post performance problems
Post #1497393

 Permissions