add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
with dates ( coddate ) as
( select convert(datetime , '15/09/2018' , 103) union all
select convert(datetime , '16/09/2018' , 103) union all
select convert(datetime , '17/09/2021' , 103) union all
select convert(datetime , '18/09/2021' , 103) union all
select convert(datetime , '19/02/2021' , 103)
) ,
rates ( rate, id ) as
(
select 'ConfD1' , 46 union all
select 'ConfD2' , 47
),
rateprices ( Rate , Id, Startdate , EndDate, price )
as
( select 'ConfD1' , 46 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '31/10/2021' , 103 ), 111 union all
select 'ConfD1' , 46 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '01/03/2022' , 103 ), 222 union all
select 'ConfD2' , 47 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '31/10/2021' , 103 ), 333 union all
select 'ConfD2' , 47 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '01/03/2022' , 103 ), 444 union all
select 'ConfD3' , 48 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '31/10/2021' , 103 ), 555 union all
select 'ConfD3' , 48 , convert(datetime , '01/01/2021' , 103) , convert(datetime , '01/03/2022' , 103 ), 666
),
rates_and_dates ( rate , id , coddate )
as
( select a.rate , a.id , b.coddate from rates a cross join dates b
),
result ( rate, id, coddate, price )
as
(
select a.rate , a.id , a.coddate , price = coalesce(b.price, 0)
from rates_and_dates a inner join rates c
on a.rate=c.rate
left join rateprices b
on a.rate=b.rate and b.startdate <= a.coddate and b.enddate >= a.coddate
)
select rate, coddate, price from result
order by 2, 1
rate coddate price
ConfD1 2018-09-15 00:00:00.000 0
ConfD2 2018-09-15 00:00:00.000 0
ConfD1 2018-09-16 00:00:00.000 0
ConfD2 2018-09-16 00:00:00.000 0
ConfD1 2021-02-19 00:00:00.000 111
ConfD1 2021-02-19 00:00:00.000 222
ConfD2 2021-02-19 00:00:00.000 333
ConfD2 2021-02-19 00:00:00.000 444
ConfD1 2021-09-17 00:00:00.000 111
ConfD1 2021-09-17 00:00:00.000 222
ConfD2 2021-09-17 00:00:00.000 333
ConfD2 2021-09-17 00:00:00.000 444
ConfD1 2021-09-18 00:00:00.000 111
ConfD1 2021-09-18 00:00:00.000 222
ConfD2 2021-09-18 00:00:00.000 333
ConfD2 2021-09-18 00:00:00.000 444