sql >> Base de Datos >  >> RDS >> Oracle

SQL trunc/agrupar/ordenar por fechas (día/mes/trimestre/año) con suma de fechas salteadas sin datos

Prueba algo como esto (ejemplo 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

Aquí hay un ejemplo de sqlfiddle