sql server - sql query : double record for joins ... can;t figure it out -


i have tables :

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  --- child 3         564522       ss4433  -- 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 how select 1 query parent , it's children in same select (based on id_share in share table), contains group of parent.

the result should 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 .. can't figure out how .. without union all

i have tried :

select a.id_client, a.id_group, ??package?? , id_contract , ??child_of?? parent_table  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: need find parents , childs corespond 2012-01-01 - 2012-01-31 interval

where have put ?? don;t know .

thanks

updated, restrict parents , children date:

select a.id_client,        a.id_group,        coalesce(d.package, a.package) package,        coalesce(d.start_date, a.start_date) start_date,        coalesce(d.end_date, a.end_date) end_date,        coalesce(d.id_contract, a.id_contract)  id_contract,        case when d.package not null a.package else 0 end child_of parent_table  join share_table b on b.package = a.package join share_table c on c.id_share = b.id_share left join child_table d on d.package = c.package ,                            d.start_date <= '2012-01-31' ,                            d.end_date >= '2012-01-01'  a.start_date <= '2012-01-31' ,       a.end_date >= '2012-01-01' ,       (d.package not null or a.package = c.package) 

Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -