我有一些像这样的表:
予。父表:
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。分享表:
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。子表:
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
问题是如何使用一个查询选择同一个选择中的父项及其所有子项(基于共享表中的id_share),其中包含父项的组。
结果应如下所示:
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
我已经尝试过各种方式..但我无法弄清楚如何做到这一点......没有工会全部
我试过这个:
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:我需要找到对应于2012-01-01 - 2012-01-31间隔的父母和孩子
我放的地方?我不知道 。
由于
答案 0 :(得分:1)
更新,按日期限制父母和子女:
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 is not null then a.package else 0 end 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
left join child_table d on d.package = c.package and
d.start_date <= '2012-01-31' and
d.end_date >= '2012-01-01'
where a.start_date <= '2012-01-31' and
a.end_date >= '2012-01-01' and
(d.package is not null or a.package = c.package)
答案 1 :(得分:0)
这个怎么样。
select p.id_client, p.id_group, s.package, c.start_date, c.id_contract
from parent p right outer join share s
on p.package = s.package
right outer join child c
on p.package = c.package;
答案 2 :(得分:0)
联合似乎是一种更好的方法,但可以使用isnull和外连接...
select isnull(p.id_client, pofc.id_client) id_client, isnull(p.id_group, pofc.id_group) id_group, isnull(p.package, c.package) package, isnull(p.startDate, c.startDate), isnull(p.endDate, c.endDate) endDate, isnull(p.id_contract, c.id_contract) id_contract, isnull(pofc.package, 0) child_of
from share s
left outer join parent p on p.package = s.package and p.startDate <= @toDate and p.endDate >= @fromDate
left outer join child c on c.package = s.package and c.startDate <= @toDate and c.endDate >= @fromDate
left outer join (select ps.id_share, p.package, p.id_client, p.id_group from share ps inner join parent p on p.package = ps.package) pofc on pofc.id_share = s.share and pofc.package <> s.package
where p.package is not null
or c.package is not null