I need to do something really weird, which is to create fake records in a view to fill the gap between posted dates of product prices.
Actually, my
I just realized that @Wolf and @jonearles improvements do not return the exact results I needed because the row generator to list all dates won't generate the ranges by product. If the first price of product A is later than any price of product B the first listed date of product A still must be the same. But they really helped me to work further and get the expected results:
I started with changing @wolf's date range selector from this:
select min(price_date) beg_date, sysdate end_date from prices_test
to this:
select min(PRICE_DATE) START_DATE, sysdate as END_DATE, PRODUCT
from PRICES_TEST group by sysdate, PRODUCT
But, somehow, the number of rows per product is exponentially growing repeatedly for each level. I just added a distinct in the outter query. The finally select was this:
select
DP.PRICE_DATE,
DP.PRODUCT,
LAST_VALUE(PT.PRICE ignore nulls) over (order by DP.PRODUCT, DP.PRICE_DATE) PRICE
from (
select distinct START_DATE + DAYS as PRICE_DATE, PRODUCT
from
(
-- Row generator to list all dates from first date of each product to today
with DATES as (select min(PRICE_DATE) START_DATE, sysdate as END_DATE, PRODUCT from PRICES_TEST group by sysdate, PRODUCT)
select START_DATE, level - 1 as DAYS, PRODUCT
from DATES
connect by level < END_DATE - START_DATE + 1
order by 3, 2
) d order by 2, 1
) DP
left outer join prices_test pt on pt.price_date = dp.price_date and pt.product = dp.product;
@Mellamokb solution is actually what I really need and is certainly better than my noobie solution.
Thank's everyone not only for helping me with this but also for presenting me features such as "with" and "connect by".