By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,756 Members | 1,721 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,756 IT Pros & Developers. It's quick & easy.

how to express a recursive CTE that returns a balanced hierarchy

P: n/a
for the conext of this example imagine a simple part table where a
part can only have a single parent. try to express a cte where the
result set must be balanced. That is, the result set repeats node B
and E sufficient times that the tree is as deep as node G.

insert into Part ( PartId, PartName, ParentPart) values
( 1,'A',null), (2,'B',1), (3,'C',1), (4,'D',1), (5,'E',3), (6,'F',4),
(7,'G',6)

The following query will not parse as DB2 is complaining about types -
don't seem to be able to avoid that. If I hack the sub-query to fake
things I'm not getting the desired result. Should it be possible vs I
am presuming that one of the recursive CTEs is materialized as I'm
assuming when it should not be (ie. sql standard says my assumption is
wrong).

If this is not viable, does anyone have an example which perhaps
leverages pushing the result set into an XML frag and uses Xquery to
do the inflation I need. Note also, having to express the fake full
path is a cheezy thing as I have to define a max size, force padded
numbers etc to safely do a posstr hence the XML route might be cleaner
in general.

with
parts ( lvl, partkey, parentkey, partname, fullpath ) as
( select 0, partid, parentpart, partname, cast ( digits(p.partid) as
varchar(2000))
from part p where p.partname='A'
union all
select lvl + 1,c.partid, c.parentpart, c.partname, p.fullpath || '/'
|| digits(c.partid)
from part c, parts p where p.partkey=c.parentpart
),
leaf (partkey, fullpath) as
(
select partkey, fullpath
from parts
where lvl = ( select max (lvl) from parts )
),
balancedtree ( lvl, partkey, parentkey, partname, maxlvl ) as
(
select lvl, partkey, parentkey, partname, ( select max( lvl ) from
parts ) maxlvl
from parts
union all
select lvl + 1, partkey, parentkey, partname, maxlvl
from balancedtree b
where lvl < maxlvl and not exists (
select * from leaf l
where 0 < posstr ( l.fullpath, digits (b.partkey)
)
)
)
select lvl, partkey, partname from balancedtree

Oct 14 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Oct 14, 8:54 pm, the6campbells <the6campbe...@gmail.comwrote:
for the conext of this example imagine a simple part table where a
part can only have a single parent. try to express a cte where the
result set must be balanced. That is, the result set repeats node B
and E sufficient times that the tree is as deep as node G.

insert into Part ( PartId, PartName, ParentPart) values
( 1,'A',null), (2,'B',1), (3,'C',1), (4,'D',1), (5,'E',3), (6,'F',4),
(7,'G',6)

The following query will not parse as DB2 is complaining about types -
don't seem to be able to avoid that. If I hack the sub-query to fake
things I'm not getting the desired result. Should it be possible vs I
am presuming that one of the recursive CTEs is materialized as I'm
assuming when it should not be (ie. sql standard says my assumption is
wrong).

If this is not viable, does anyone have an example which perhaps
leverages pushing the result set into an XML frag and uses Xquery to
do the inflation I need. Note also, having to express the fake full
path is a cheezy thing as I have to define a max size, force padded
numbers etc to safely do a posstr hence the XML route might be cleaner
in general.

with
parts ( lvl, partkey, parentkey, partname, fullpath ) as
( select 0, partid, parentpart, partname, cast ( digits(p.partid) as
varchar(2000))
from part p where p.partname='A'
union all
select lvl + 1,c.partid, c.parentpart, c.partname, p.fullpath || '/'
|| digits(c.partid)
from part c, parts p where p.partkey=c.parentpart
),
leaf (partkey, fullpath) as
(
select partkey, fullpath
from parts
where lvl = ( select max (lvl) from parts )
),
balancedtree ( lvl, partkey, parentkey, partname, maxlvl ) as
(
select lvl, partkey, parentkey, partname, ( select max( lvl ) from
parts ) maxlvl
from parts
union all
select lvl + 1, partkey, parentkey, partname, maxlvl
from balancedtree b
where lvl < maxlvl and not exists (
select * from leaf l
where 0 < posstr ( l.fullpath, digits (b.partkey)
)
)
)
select lvl, partkey, partname from balancedtree

Didnt have time to investigate what it is your trying to do, but does
it help to replace:

where 0 < posstr ( l.fullpath, digits (b.partkey)

with:

where locate ( digits(b.partkey), l.fullpath ) 0?
/Lennart

Oct 15 '07 #2

P: n/a
On Oct 15, 12:34 am, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
On Oct 14, 8:54 pm, the6campbells <the6campbe...@gmail.comwrote:


for the conext of this example imagine a simple part table where a
part can only have a single parent. try to express a cte where the
result set must be balanced. That is, the result set repeats node B
and E sufficient times that the tree is as deep as node G.
insert into Part ( PartId, PartName, ParentPart) values
( 1,'A',null), (2,'B',1), (3,'C',1), (4,'D',1), (5,'E',3), (6,'F',4),
(7,'G',6)
The following query will not parse as DB2 is complaining about types -
don't seem to be able to avoid that. If I hack the sub-query to fake
things I'm not getting the desired result. Should it be possible vs I
am presuming that one of the recursive CTEs is materialized as I'm
assuming when it should not be (ie. sql standard says my assumption is
wrong).
If this is not viable, does anyone have an example which perhaps
leverages pushing the result set into an XML frag and uses Xquery to
do the inflation I need. Note also, having to express the fake full
path is a cheezy thing as I have to define a max size, force padded
numbers etc to safely do a posstr hence the XML route might be cleaner
in general.
with
parts ( lvl, partkey, parentkey, partname, fullpath ) as
( select 0, partid, parentpart, partname, cast ( digits(p.partid) as
varchar(2000))
from part p where p.partname='A'
union all
select lvl + 1,c.partid, c.parentpart, c.partname, p.fullpath || '/'
|| digits(c.partid)
from part c, parts p where p.partkey=c.parentpart
),
leaf (partkey, fullpath) as
(
select partkey, fullpath
from parts
where lvl = ( select max (lvl) from parts )
),
balancedtree ( lvl, partkey, parentkey, partname, maxlvl ) as
(
select lvl, partkey, parentkey, partname, ( select max( lvl ) from
parts ) maxlvl
from parts
union all
select lvl + 1, partkey, parentkey, partname, maxlvl
from balancedtree b
where lvl < maxlvl and not exists (
select * from leaf l
where 0 < posstr ( l.fullpath, digits (b.partkey)
)
)
)
select lvl, partkey, partname from balancedtree

Didnt have time to investigate what it is your trying to do, but does
it help to replace:

where 0 < posstr ( l.fullpath, digits (b.partkey)

with:

where locate ( digits(b.partkey), l.fullpath ) 0?

/Lennart- Hide quoted text -

- Show quoted text -
no it does not. nor do explicit casts. only literals bypass the
parsing error.

Oct 15 '07 #3

P: n/a
get a copy of TREES & HIERARCHIES ON SQ and look up the nested sets
model instead.

Oct 15 '07 #4

P: n/a
On Oct 16, 7:17 am, Lennart <Erik.Lennart.Jons...@gmail.comwrote:
[...]
Adjustments and remaining tree

with tc (node, ancestor) as (
select PartId,ParentPart from Part
union all
select PartId, ancestor from tc t, Part p
where t.node = p.ParentPart
), leafs(node) as (
select PartId from Part x
where not exists (
select 1 from Part y
where x.Partid = y.ParentPart
)
), leaf_height(node, height) as (
select tc.node, count(tc.ancestor) as height
from leafs l, tc
where tc.node = l.node
group by tc.node
), balanced_height(node,parent,height) as (
select node,node,height
from leaf_height
where height < (select max(height) from leaf_height)
union all
select node, node, height+1
from balanced_height
where height+1 < (select max(height) from leaf_height)
)
select n.partname, m.partname, x.height
from balanced_height x, part m, part n
where x.node = m.partid
and x.parent = n.partid
union all
select n.partname, m.partname,
(select count(ancestor) from tc where node = n.partid)
from part m, part n
where m.parentpart = n.partid;

/L

Oct 17 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.