Archive for May 24th, 2009
Genealogy query on both parents
Comments enabled. I *really* need your comment
From Stack Overflow:
I'm trying to fetch a genealogy tree of animals from my Oracle database.
Here's the table:
Animal_ID Parent_Male_ID Parent_Female_ID If I specify an animal, I can get all of its descendants (on the male side) using something like this:
SELECT * FROM animal START WITH animal_id = 123 CONNECT BY PRIOR animal_id = parent_male_idI'm trying to find a way to extend this in such a way that if I specify an animal, it will fetch both parents and then will fetch all of their descendants.
Any thoughts?
This is possible to do with a same CONNECT BY
query by providing a little more comlex condition for START WITH
and CONNECT BY
:
SELECT DISTINCT(animal_id) AS animal_id FROM animal START WITH animal_id IN ( SELECT father FROM animal WHERE animal_id = 9500 UNION ALL SELECT mother FROM animal WHERE animal_id = 9500 ) CONNECT BY PRIOR animal_id IN (father, mother) ORDER BY animal_id
However, my gut feeling says this query need performance checking.
Let's create a sample table:
Read the rest of this entry »