Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

SQL trunc/group/order by datas (dia/mês/trimestre/ano) com datas de salto de soma sem dados


Tente algo assim (exemplo simplificado):
with 
months_int as
(select trunc(min(inc_date), 'MM') min_month, trunc(max(inc_date), 'MM') max_month
 from data),
months as
(
  select add_months(min_month, level-1) mnth_date
  from months_int 
  connect by add_months(min_month, level-1)<= max_month
  )
select  mnth_date, sum(cnt) 
from data  right outer join months on trunc(inc_date, 'MM') = mnth_date
group by mnth_date
order by mnth_date

Aqui está um exemplo de sqlfiddle