Author |
Topic |
vpawa
Starting Member
3 Posts |
Posted - 2012-11-05 : 07:13:58
|
Hi,Im trying to loop my query but dont know how to do. can somebody help me to improve the query?I have created the following query.SELECT MEIT.client, MEIT.tree_type, 0 AS tree_level, MEIT.menu_id, MEIT.parent_menu_id, MEIT.menu_type, MEIT.licence_ref, MEIT.icon_type, MEIT.title_nl, MEIT.title_en, MEIT.title_fr, MEIT.title_de, CONVERT(NVARCHAR, RIGHT(N'0000' + CONVERT(NVARCHAR, ROW_NUMBER() OVER (PARTITION BY MEIT.client, MEIT.tree_type ORDER BY CASE WHEN MEIT.menu_type = 5 THEN 5 WHEN MEIT.menu_type = 6 THEN 6 ELSE 1 END, MEIT.sequence_no, MEIT.title_no)),4)) AS tree_sequence FROM uvimenuitem MEIT WHERE MEIT.parent_menu_id = N'!' UNION ALL SELECT MEIT.client, MEIT.tree_type, TREES.tree_level+1 AS tree_level, MEIT.menu_id, MEIT.parent_menu_id, MEIT.menu_type, MEIT.licence_ref, MEIT.icon_type, MEIT.title_nl, MEIT.title_en, MEIT.title_fr, MEIT.title_de, CONVERT(NVARCHAR, TREES.tree_sequence + RIGHT(N'0000' + CONVERT(NVARCHAR, ROW_NUMBER() OVER (PARTITION BY MEIT.client, MEIT.tree_type ORDER BY CASE WHEN MEIT.menu_type = 5 THEN 5 WHEN MEIT.menu_type = 6 THEN 6 ELSE 1 END, MEIT.sequence_no, MEIT.title_no)),4)) AS tree_sequence FROM uvimenuitem MEIT INNER JOIN tree_structure TREES ON TREES.client = MEIT.client AND TREES.tree_type = MEIT.tree_type AND TREES.menu_id = MEIT.parent_menu_idnow im trying to loop TREES.tree_level+1 10 times. does somebody knows how to get that withoud creating 10 view tables?ThanksVishal |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-05 : 07:30:31
|
I didn't quite follow the logic you are trying to implement. You may need a recursive query if the results of each level is used to compute the results of the next level. The following can be used if you just want to run the same query ten times, but with n=1,2,..10.....UNION ALLSELECT MEIT.client, MEIT.tree_type, TREES.tree_level + n AS tree_level, MEIT.menu_id,....FROM ( VALUES (1) ,(2),(3),(4),(5),(6),(7),(8),(9),(10) )s(n) CROSS JOIN ( uvimenuitem MEIT AND TREES.menu_id = MEIT.parent_menu_id .... ) |
|
|
vpawa
Starting Member
3 Posts |
Posted - 2012-11-05 : 07:59:13
|
I need it to compute the next tree level. I tought the most easy way is to loop the query, but im not getting the result what im looking for with the query. Do you hav a suggestion how to calulate the different levels?thanksquote: Originally posted by sunitabeck I didn't quite follow the logic you are trying to implement. You may need a recursive query if the results of each level is used to compute the results of the next level. The following can be used if you just want to run the same query ten times, but with n=1,2,..10.....UNION ALLSELECT MEIT.client, MEIT.tree_type, TREES.tree_level + n AS tree_level, MEIT.menu_id,....FROM ( VALUES (1) ,(2),(3),(4),(5),(6),(7),(8),(9),(10) )s(n) CROSS JOIN ( uvimenuitem MEIT AND TREES.menu_id = MEIT.parent_menu_id .... )
|
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-11-05 : 08:06:23
|
I must admit that I didn't follow the logic you are trying to implement. What I posted earlier was a wild guess, and I am not surprised it didn't work. Can you post a simplified version (with may be fewer columns) and some sample data that is in your input tables and the output you are trying to get? |
|
|
vpawa
Starting Member
3 Posts |
Posted - 2012-11-05 : 08:29:42
|
Can i get your mailadress so i can send you some more information.quote: Originally posted by sunitabeck I must admit that I didn't follow the logic you are trying to implement. What I posted earlier was a wild guess, and I am not surprised it didn't work. Can you post a simplified version (with may be fewer columns) and some sample data that is in your input tables and the output you are trying to get?
|
|
|
karthik0805
Starting Member
14 Posts |
Posted - 2012-11-06 : 02:41:59
|
DECLARE @A INTSET @A=1SELECT MEIT.client, MEIT.tree_type, 0 AS tree_level, MEIT.menu_id, MEIT.parent_menu_id, MEIT.menu_type, MEIT.licence_ref, MEIT.icon_type, MEIT.title_nl, MEIT.title_en, MEIT.title_fr, MEIT.title_de,CONVERT(NVARCHAR, RIGHT(N'0000' + CONVERT(NVARCHAR, ROW_NUMBER() OVER (PARTITION BY MEIT.client, MEIT.tree_type ORDER BY CASE WHEN MEIT.menu_type = 5 THEN 5 WHEN MEIT.menu_type = 6 THEN 6 ELSE 1 END, MEIT.sequence_no, MEIT.title_no)),4)) AS tree_sequenceFROM uvimenuitem MEITWHERE MEIT.parent_menu_id = N'!'UNION ALLWHILE @A<=10BEGINSELECT MEIT.client, MEIT.tree_type, TREES.tree_level+@A AS tree_level, MEIT.menu_id, MEIT.parent_menu_id, MEIT.menu_type, MEIT.licence_ref, MEIT.icon_type, MEIT.title_nl, MEIT.title_en, MEIT.title_fr, MEIT.title_de,CONVERT(NVARCHAR, TREES.tree_sequence + RIGHT(N'0000' + CONVERT(NVARCHAR, ROW_NUMBER() OVER (PARTITION BY MEIT.client, MEIT.tree_type ORDER BY CASE WHEN MEIT.menu_type = 5 THEN 5 WHEN MEIT.menu_type = 6 THEN 6 ELSE 1 END, MEIT.sequence_no, MEIT.title_no)),4)) AS tree_sequenceFROM uvimenuitem MEITINNER JOIN tree_structure TREES ON TREES.client = MEIT.client AND TREES.tree_type = MEIT.tree_type AND TREES.menu_id = MEIT.parent_menu_idSET @A=@A+1END |
|
|
Jeff Moden
Aged Yak Warrior
652 Posts |
Posted - 2012-11-11 : 18:30:00
|
quote: Originally posted by vpawa Hi,Im trying to loop my query but dont know how to do. can somebody help me to improve the query?I have created the following query.SELECT MEIT.client, MEIT.tree_type, 0 AS tree_level, MEIT.menu_id, MEIT.parent_menu_id, MEIT.menu_type, MEIT.licence_ref, MEIT.icon_type, MEIT.title_nl, MEIT.title_en, MEIT.title_fr, MEIT.title_de, CONVERT(NVARCHAR, RIGHT(N'0000' + CONVERT(NVARCHAR, ROW_NUMBER() OVER (PARTITION BY MEIT.client, MEIT.tree_type ORDER BY CASE WHEN MEIT.menu_type = 5 THEN 5 WHEN MEIT.menu_type = 6 THEN 6 ELSE 1 END, MEIT.sequence_no, MEIT.title_no)),4)) AS tree_sequence FROM uvimenuitem MEIT WHERE MEIT.parent_menu_id = N'!' UNION ALL SELECT MEIT.client, MEIT.tree_type, TREES.tree_level+1 AS tree_level, MEIT.menu_id, MEIT.parent_menu_id, MEIT.menu_type, MEIT.licence_ref, MEIT.icon_type, MEIT.title_nl, MEIT.title_en, MEIT.title_fr, MEIT.title_de, CONVERT(NVARCHAR, TREES.tree_sequence + RIGHT(N'0000' + CONVERT(NVARCHAR, ROW_NUMBER() OVER (PARTITION BY MEIT.client, MEIT.tree_type ORDER BY CASE WHEN MEIT.menu_type = 5 THEN 5 WHEN MEIT.menu_type = 6 THEN 6 ELSE 1 END, MEIT.sequence_no, MEIT.title_no)),4)) AS tree_sequence FROM uvimenuitem MEIT INNER JOIN tree_structure TREES ON TREES.client = MEIT.client AND TREES.tree_type = MEIT.tree_type AND TREES.menu_id = MEIT.parent_menu_idnow im trying to loop TREES.tree_level+1 10 times. does somebody knows how to get that withoud creating 10 view tables?ThanksVishal
You probably need to use an rCTE (Recursive CTE) for this. Books Online explains how to use one. The following URL explains how to traverse a simple tree in an ordered fashion.[url]http://www.sqlservercentral.com/articles/T-SQL/72503/[/url]--Jeff Moden |
|
|
|
|
|