Oracle
 sql >> база данни >  >> RDS >> Oracle

Как да попълня липсващите дати по групи в Oracle

Можете да използвате Рекурсивно факториране на подзаявки за генериране на интервали:

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. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Регулярният израз на Oracle съвпада с интервал

  2. Грешка в тригера на ORACLE

  3. Как мога да добавя водещи нули към дати в Oracle?

  4. Премахнете числата, намерени в колоната с низове

  5. 11 начина за намиране на дублиращи се редове, които имат първичен ключ в Oracle