How to get all the parents of a given node from a tree in single row?
HI,
I have a requirement to get the all the parent nodes till the root, of all the leaf nodes in a Tree structure in a single row instead of multiple rows.
Normally we can get the tree structure as follows for a leaf level node_id : 1023.
select node_id,node_name,node_level,parent_node_id
from tree
start with node_id=1023
connect by prior parent_node_id=node_id
This query returns all the parents of the leaf nodes, but in multiple rows as follows
Re: How to get all the parents of a given node from a tree in single row?
Code:
SELECT MAX(node_company),
MAX(node_state),
MAX(node_region),
MAX(node_district)
FROM
(SELECT DECODE(node_level,1,node_name) node_company,
DECODE(node_level,2,node_name) node_state,
DECODE(node_level,3,node_name) node_region,
DECODE(node_level,4,node_name) node_district
FROM
(select node_id,node_name,node_level,parent_node_id
from tree
start with node_id=1023
connect by prior parent_node_id=node_id
)
);
The assumption here is that each leaf node you are making the report for is at exactly fourth level, ie, it allways has three parent levels.
Last edited by jmodic; 09-14-2004 at 11:42 AM.
Jurij Modic ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
Your query is working fine for one leaf node, but if I give more than one leaf node in the query, its loading parents information of only one leaf.
I have tried as follows...
Code:
SELECT MAX(node_company),
MAX(node_state),
MAX(node_region),
MAX(node_district)
FROM
(SELECT DECODE(node_level,1,node_name) node_company,
DECODE(node_level,2,node_name) node_state,
DECODE(node_level,3,node_name) node_region,
DECODE(node_level,4,node_name) node_district
FROM
(select node_id,node_name,node_level,parent_node_id
from tree
start with node_id in (1023,1080,1040)
connect by prior parent_node_id=node_id
)
);
Originally posted by srinivasm How we can achive it for multiple leaf nodes ?
Look at padders's suggestions. In case for some reason you can't (or won't) use analytical functions, you can still use my method, only slightly modified:
Code:
SELECT MAX(node_company),
MAX(node_state),
MAX(node_region),
MAX(node_district)
FROM
(SELECT DECODE(node_level,1,node_name) node_company,
DECODE(node_level,2,node_name) node_state,
DECODE(node_level,3,node_name) node_region,
DECODE(node_level,4,node_name) node_district,
CEIL(ROWNUM/4) bucket
FROM
(SELECT node_id,node_name,node_level,parent_node_id
FROM tree
START WITH node_id IN (1023,1080)
CONNECT BY PRIOR parent_node_id=node_id
)
)
GROUP BY bucket;
Jurij Modic ASCII a stupid question, get a stupid ANSI
24 hours in a day .... 24 beer in a case .... coincidence?
How we can achive it for multiple nodes without using the analytical functions?
Hi JModic,
Thank you for your query.
When I try the padder's query(which has the analytical functions),
the performance is very slow for large number of nodes(almost 1967 nodes) in the "in clause".
Then I have tried your query without analytical functions.
But I have one problem your query, that the nodes in the "IN clause" are not always level 4 nodes,
those may be any level from 4 to 1.
If the node in the "IN clause" is 4 then it returns 4,3,2,1.
If the node in the "IN clause" is 3 then it returns 3,2,1.
If the node in the "IN clause" is 2 then it returns 2,1.
If the node in the "IN clause" is 1 then it returns 1.
For the above scenario's CEIL(ROWNUM/4) doesn't work to group the results.
How we can acheive this without using the analytical functions?
Bookmarks