Skip to content
Advertisement

Recursive query in Oracle – length of chain

i’ve got table with some posts. Post have fields: post_id (key) and parent_post_id (can be null). ( If post A is replying to post B then A.parent_post_id = B.post_id). My problem is: write query to oracle db to get ‘chain length’ for every post. I mean: for every post_id i want to know how many nested child there are.(So it’s more like tree size, but chain example would be helpful too) I have tried some recursive queries but i this case i helpless.

Fe. if arrow ( <-) means is parent. For A <- B <- C <- D I want to get:

Advertisement

Answer

Hierarchical queries are made for your case. I slightly extended your tree in the following way:

If I understand you correctly, the following are the desired results:

Here is the query:

Hope this helps.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement