I have a table like this : (note that id_pack is not auto incremented)
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
where id_pack = 3 is the child of the two parents. I want to make a query to select the parents and the child for the month 2012-01 but the child needs to be doubled (because his first parent finished on 2012-01-18). So the result needs to look like this :
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
I have tried in every way and I can’t figure it out. I’m doing this because parents are assigned a price rate in another table, and for the current month the child had two parents with different price rates, so I need to charge from 2012-01-01 : 2012-01-18 using a rate plan and from 2012-01-18 : 2012-01-31 using another rate plan.
Is this even possible with one query ?
Thank you
PS: I have something like this :
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'
Try: