473,379 Members | 1,170 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

how to express a recursive CTE that returns a balanced hierarchy

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
4 3576
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
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
get a copy of TREES & HIERARCHIES ON SQ and look up the nested sets
model instead.

Oct 15 '07 #4
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
13
by: Timothy Madden | last post by:
Hello all I'm facing a very strange problem. I need to define a function that takes as argument something like a pointer to itself. It takes as argument a pointer of the same type as a pointer...
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take...
20
by: Gary Manigian | last post by:
I have 2 tables, one-to-many, that contain bills of material(BOMs): tblBOM: lngBOMID (PK) strAssemblyPartNo strDescription tblBOMDetail: lngBOMDetailID (PK) lngBOMID (FK)
7
by: abhrajit | last post by:
I'm looking for a C/C++/Java library to create a balanced binary tree data structure given a set of leaf nodes as input. A leaf node should never become an interior node. So if I wish to create...
8
by: Andy | last post by:
Hi all, i am pretty new to programming and have a (simple?) problem here: I want to populate a Treeview with data from a table. Table layout is: ID, ParentID, Description There can be...
2
by: sebastien.abeille | last post by:
Hello, I would like to create a minimalist file browser using pyGTK. Having read lot of tutorials, it seems to me that that in my case, the best solution is to have a gtk.TreeStore containing...
3
by: fabiomoggi | last post by:
Hello Guys, I am developing a web application to manage Active Directory resources, and one of my tasks is to map Organizational Units hierarchy into a SQL Server database. Let's suppose that I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.