I have records with a project id and an ‘inherit id’, i.e. the project that the current project inherits from. The inheritance level is unknown.
I now need to collapse this to: ‘inherit from the top most’
E.g.:
1006 <- 1005 <- 1002 <- 999
prj_id / inherit_id
999 / 1002
1002 / 1005
1005 / 1006
should collapse to
1006 <- 1005
1006 <- 1002
1006 <- 999
prj_id / inherit_id
999 / 1006
1002 / 1006
1005 / 1006
Can this be done in SQL statement without loops?
Creating temporary tables is fine.
It should work for FireBird, SQL Server, Oracle 9+ (i.e. 3 sets of statements is fine)
I only got this far:
Records that inherit from a record that itself inherits again:
select tt_prj_id,tt_name,tt_inherit_id from tt_prj a
where a.tt_inherit_id in
(select tt_prj_id from tt_prj b
where b.tt_inherit_id is not null
and b.tt_inherit_id > 0)
Who can help me further?
Something like the following should get you started (it’s not the final solution!):
This works Firebird 2.5 (might also work with 2.1, but I don’t have one at hand right now)
It should work with SQL Server when you remove the keyword
recursive(which is required by the ANSI standard, but since when did Microsoft care about that…) and you need to replace the standard string concatention||with the+operator.Oracle does only support recursive CTEs before 11.2.
In prior version you need to rewrite this using
CONNECT BY, which woudl be something like this: