Duplicating records to fill gap between dates

前端 未结 4 768
我在风中等你
我在风中等你 2020-12-31 17:43

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

4条回答
  •  萌比男神i
    2020-12-31 18:29

    You can create a row generator statement using the CONNECT BY LEVEL syntax, cross joined with the distinct products in your table, and then outer join that to your prices table. The final touch is to use the LAST_VALUE function and IGNORE NULLS to repeat the price until a new value is encountered, and since you wanted a view, with a CREATE VIEW statement:

    create view dense_prices_test as
    select
        dp.price_date
      , dp.product
      , last_value(pt.price ignore nulls) over (order by dp.product, dp.price_date) price
    from (
          -- Cross join with the distinct product set in prices_test
          select d.price_date, p.product
          from (
                -- Row generator to list all dates from first date in prices_test to today
                with dates as (select min(price_date) beg_date, sysdate end_date from prices_test)
                select dates.beg_date + level - 1 price_date 
                from dual
                cross join dates
                connect by level <= dates.end_date - dates.beg_date + 1
                ) d
          cross join (select distinct product from prices_test) p
         ) dp
    left outer join prices_test pt on pt.price_date = dp.price_date and pt.product = dp.product;
    

提交回复
热议问题