统计12个月前
SELECT TO_CHAR(ADD_MONTHS(TO_DATE(to_char(TRUNC(add_months(trunc(sysdate),-1),'MM'),'yyyy-mm'),'yyyy-mm'), 1-ROWNUM),'yyyy-MM') AS calcmonth FROM dual
CONNECT BY ROWNUM <= 12
sql结果
时间范围自定义
统计201912~201901,月份数 之前的月
SELECT TO_CHAR(ADD_MONTHS(TO_DATE('2021-10','yyyy-mm'), 1-ROWNUM),'yyyy-MM') FROM dual
CONNECT BY ROWNUM <= MONTHS_BETWEEN(TO_DATE('201912','yyyyMM'), TO_DATE('201901','yyyyMM'))
sql结果