Oracle
 sql >> Database >  >> RDS >> Oracle

SQL trunc/group/order by date (giorno/mese/trimestre/anno) con date di salto somma senza dati

Prova qualcosa del genere (esempio semplificato):

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

Ecco un esempio di sqlfiddle