我有一个这样的表:(请注意,id_pack不会自动递增)
id_pack start_date end_date is_parent id_contract
1 2011-11-01 2012-01-18 1 5547
2 2012-01-18 2050-01-01 1 5547
3 2009-02-02 2050-01-01 0 5547
其中id_pack = 3
是两个父母的孩子。我想查询选择2012-01学年的父母和孩子,但是孩子需要加倍(因为他的第一个父母在2012-01-18完成)。所以结果需要看起来像这样:
id_pack start_date end_date id_parent
1 2012-01-01 2012-01-18 0
2 2012-01-18 2012-01-31 0
3 2012-01-01 2012-01-18 1
3 2012-01-18 2012-01-31 2
我已经尝试过各种方式,我无法弄明白。我这样做是因为父母在另一张桌子上被分配了价格,而且当月孩子有两个父母的价格不同,所以我需要从2012-01-01:2012-01-18收取费用费率计划和2012-01-18:2012-01-31使用另一个费率计划。
一次查询是否可以实现?
谢谢
PS:我有这样的事情: select c.id_pack,
case when c.start_date < '2012-01-01' then '2012-01-01'
else c.start_date
end as start_date,
case end date ...... the same as start_date as end_date,
from client a
join contract b on b.id_client = a.id_client
join package c on c.id_contract = b.id_contract
and c.start_date < dateadd(mm,1,'2012-01-01')
and c.end_date >= '2012-01-01'
where a.id_clinet = '12345'
答案 0 :(得分:0)
尝试:
select c.id_pack, p.start_date, p.end_date, coalesce(p.id_pack,0) id_parent
from package c
left join package p
on c.contract_id = p.contract_id and p.is_parent = 1 and c.is_parent = 0