I have some tables like this :
I. parent table :
id_client id_group package start_date end_date id_contract is_parent
1223 88 1234 2012-01-01 2050-01-01 156447 1
1223 89 34342 2011-04-01 2050-01-01 156447 1
II. share table :
id package id_share
1 1234 SS4433 - parent
2 564679 SS4433 --- this is a child
3 564522 SS4433 -- this is a child
4 34342 SS2345 - parent
5 665456 SS2345 -- child
6 7789997 SS2345 -- child
III. child table :
package start_date end_date id_contract
564679 2011-01-01 2012-02-01 156447
564522 2011-01-01 2011-05-07 156447
665456 2011-01-01 2012-02-04 156447
7789997 2011-01-01 2011-07-03 156447
The question is how to select with one query the parent and all it’s children in the same select (based on id_share in share table), that contains the group of the parent.
The result should look like this:
id_client id_group package start_date end_date id_contract child_of
1223 88 1234 2012-01-01 2050-.. 156447 0
1223 88 564679 2011-01-01 2012-02-01 156447 1234
1223 89 34342 2011-04-01 2050-... 156447 0
1223 89 665456 2011-01-01 2012-02-04 156447 34342
I have tried in every way .. but I can’t figure it out how to do it .. without union all
I have tried this :
select a.id_client, a.id_group, ??package?? , id_contract , ??child_of??
from parent_table a
join share_table b on b.package = a.package
join share_table c on c.id_share = b.id_share
join child_table d on d.package = c.package
PS: I need to find parents and childs that corespond to 2012-01-01 – 2012-01-31 interval
where i have put ?? i don;t know .
Thanks
UPDATED, to restrict parents and children by date: