sql >> Database >  >> RDS >> Oracle

Hoe vul ik ontbrekende datums in per groep in Oracle

U kunt Recursieve Subquery Factoring gebruiken om intervallen te genereren:

with tbl as (
    select to_date('1/28/2020 11:51', 'MM/DD/YYYY HH24:MI') as color_date, 'red' as color  from dual union
    select to_date('2/3/2020 11:51', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color  from dual union
    select to_date('2/6/2020 11:51', 'MM/DD/YYYY HH24:MI') as color_date,  'red' as color  from dual union
    select to_date('4/16/2020 11:51', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color from dual union
    select to_date('4/19/2020 11:51', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color from dual union
    select to_date('4/23/2020 11:51', 'MM/DD/YYYY HH24:MI') as color_date, 'blue' as color from dual union
    select to_date('5/2/2020 11:51', 'MM/DD/YYYY HH24:MI') as color_date,  'blue' as color from dual
),
tbl_min_max as (
    select t.color, min(trunc(color_date)) begin_interval, max(trunc(color_date)) end_interval from tbl t group by t.color
),
tbl_interval(color, begin_interval, end_interval, color_date) as (
    select color, begin_interval, end_interval, begin_interval from tbl_min_max
    union all 
    select color, begin_interval, end_interval, color_date + 1 from tbl_interval where color_date < end_interval 
)
select 
    t.color, t.color_date
from 
    tbl_interval t
order by
    t.color, t.color_date



  1. Gegevens exporteren van SQL Server 2005 naar MySQL

  2. Creëert een UNIEKE constraint automatisch een INDEX op het/de veld(en)?

  3. ORA-00604-fout bij het invoegen van batches in TransactionScope

  4. Kan geen mysql-gegevens ophalen (CodeIgniter selecteren)