sysdateからカレンダーみたいなSQL

月次集計とかするときに、レコードが全く無い月があると

YYYYMM01     MTOTAL
-------- ----------
05-08-01       3150
05-12-01      18927
06-01-01       6331
06-03-01       3589

みたいに、歯抜けになるのがちょっと嫌。そんな時には

select add_months(startm, rownum) yyyymm01 from
(select TRUNC(sysdate - 12,'MM') startm from dual) ,dict
where rownum <=12

こんなカレンダーみたいなものを作ってjoinすると便利かも。

YYYYMM01     MTOTAL
-------- ----------
05-07-01          0
05-08-01       3150
05-08-01          0
05-09-01          0
05-10-01          0
05-11-01          0
05-12-01      18927
06-01-01       6331
06-02-01          0
06-03-01       3589
06-04-01          0
06-05-01          0
06-06-01          0

逆順なら

select add_months(endm, 1 - rownum) yyyymm01 from
(select TRUNC(sysdate,'MM') endm from dual) ,dict
where rownum <=12

10分毎なら

select to_char((starthm + (rownum-1)/144),'hh24mi') hh24mi10 from
(select trunc(sysdate) starthm from dual),dict
where rownum<=144;