Duplicating records to fill gap between dates

前端 未结 4 761
我在风中等你
我在风中等你 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条回答
  •  既然无缘
    2020-12-31 18:41

    I made a few changes to Wolf's excellent answer.

    I replaced the subquery factoring (WITH) with a regular subquery in the connect by. This makes the code a little simpler. (Although this type of code looks weird at first either way, so there may not be a huge gain here.)

    Most significantly, I used a partition outer join instead of a cross join and outer join. Partition outer joins are also kind of strange, but they are meant for exactly this type of situation. This makes the code simpler, and should improve performance.

    select
        price_dates.price_date
        ,product
        ,last_value(price ignore nulls) over (order by product, price_dates.price_date) price
    from
    (
        select trunc(sysdate) - level + 1 price_date
        from dual
        connect by level <= trunc(sysdate) -
            (select min(trunc(price_date)) from prices_test) + 1
    ) price_dates
    left outer join prices_test
        partition by (prices_test.product)
        on price_dates.price_date = prices_test.price_date;
    

提交回复
热议问题