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


Recursive Queries in SQL:1999 and SQL Server 2005


Recursive Queries in SQL:1999 and SQL Server 2005

Author
Message
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)SSC Guru (114K reputation)

Group: General Forum Members
Points: 114655 Visits: 41391
Very, very nice. I do a lot with Hierarchies and this will really help when 2005 comes out. Your examples and text based diagrams are awsome. Thank you very much for taking the time to do this. We need to get someone to translate your books to English...

--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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Wayne Lawton
Wayne Lawton
SSC-Addicted
SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)SSC-Addicted (424 reputation)

Group: General Forum Members
Points: 424 Visits: 22

I apologize in advance if this set of questions takes the topic too far off track. We can easily take the conversations to a different thread if appropriate.

Excellent article. It is very clear that 2005 will greatly simplify traversing hierarchies and networks, at least from a syntax perspective.

Most of my work uses multiple orthoganal hierarchies and networks as a way to aggregate and analyze large volumes of data. For example, consider an organization hierarchy that is related to but not dependent on a geographical network. Using 2005, what happens to performance as we add this additional real world complexity?

In data warehouse terminology, the organization tree is a slowly changing dimension. The geographic tree and the time tree are static. Data is aggregated and analyzed along the organization tree, along the geographic tree, and along the "time" tree.

How might one change the examples to walk two or more trees at the same time? For example, suppose one wanted to stop in towns along the way and buy a bottle of wine. One would need to store the cost of a bottle of wine in each town, for each day, over the last few months. The "best" route then would consider not only the physical distance, but also the average cost of a bottle of wine over the last two weeks. This example requires us to walk the geography tree and the time tree at the same time in order to calculate the "best" route.

In my real work, there are actually nine dimensions of interest. Three of the dimensions are slowly changing dimensions, two are static dimensions, and four are flat dimensions. The dimensions are a mix of hierarchies and networks. Heterogeneous devices submit data on time intervals varying from sub-second to daily. My current approach is to use recursive algorithms to populate what Kimball refers to as bridge tables, and then associate the aggregate values with nodes in the bridge. Once collected, the data is stored in the warehouse and available for ad hoc queries for two years. Yes, there are multiple terabytes of data online for ad hoc query and analysis.

Given this problem space, how might one modify the examples to efficiently aggregate the data "on demand" rather than storing redundant data for each node in the hierarchies and networks?

Thanks in advance for any thoughts

Wayne





Frédéric BROUARD
Frédéric BROUARD
SSC Rookie
SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)SSC Rookie (39 reputation)

Group: General Forum Members
Points: 39 Visits: 129
I am no shure to have well understand your problem. (My english is very far away in my mind...) But I think that valuated the nodes is perhaps not the good solution.
You can valuate the branches...

This can be done by adding a weight on the branches of the tree.
A weight can be constituted as a vector, wich allow as many dimension as you want, but produce a final result in term of "cost".

Let me give you an example wich is always about roads.
In france we have very small road call "chemin vicinal" with are close to lane. After we have "route departementale" wich is a step over managing by the department (equivalent of state in US), after we get "route nationale" wich are deliver and manage by the country and over we have "autoroute" wich are speedway.
The speed is limit as :
Type of road Km/h Average reduce factor
Vicinal 50 0,8
Departementale 90 0,9
Nationale 90 0,9
Nationale 4 ways 110 0,95
Autoroute 130 0,98

Then we can have some factors that are about where the road is : mountain, or flat land
Flat : 1
Small mountains : 0,85
Heavy mountains : 0,75

Then we can have some factors that are about the quality of surface of the road :
Flat : 1
Small holes : 0,9
Bad surface : 0,75

All thoses factors can be specify for all the branches of the tree. Then we can calculate the real travel time, and deduce that a Mac Truck model 125477 will cost 5444$ to go from Paris to Toulouse with 23t of duties

Is that transposable to your model ?

Thanks



hot2use
hot2use
Old Hand
Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)Old Hand (331 reputation)

Group: General Forum Members
Points: 331 Visits: 71

How did I ever miss this one!
Brilliant. Thanks ever so much.




__________________________________
Searching the KB articles is like
picking your nose. You never know
what you'll find.

perreault
perreault
Valued Member
Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)Valued Member (60 reputation)

Group: General Forum Members
Points: 60 Visits: 20

Merci Frédéric. Et la tristesse, je n'ai que SQL 2000.

And since your English is much better than my French, I'll just add that I concur with others here. Your article is one of the best I've seen on the subject. Thanks!





ObiWan Kenoby
ObiWan Kenoby
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
What can i say.. I think is the best article i have read about using recursive queries in resolving problems. It was exactly what i was looking for. Thanks again for this great article!
meg evans
meg evans
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1

This is a great article, but I am running into issues with trying to apply this to my own querey.

if anyone is following this string, could you look at the following and tell me if anything jumps out at you for where i am going wrong?

With tree (id, name) AS

(Select id, name

from clients

where parent_id=1

Union all (

Select id, name

from clients

INNER JOIN tree t

ON t.parent_id = t.id)

Select * from tree)

many thanks from one who is just working on this part of the database.


JPJ
JPJ
SSC Rookie
SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)SSC Rookie (46 reputation)

Group: General Forum Members
Points: 46 Visits: 30

Frédéric BROUARD,

Your article is awesome. It provides solutions to all my boggling SQL dilemmas.

Thanks for sharing your wisdom.

Jimi J


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