给你这样一个函数,不知道够用不够用:
create or replace function my_add_months(p_date varchar2,p_step number) return varchar2 is
v_rtnValue varchar2(20);
begin
v_rtnValue:=substr(p_date,7);
v_rtnValue:=to_char(add_months(to_date(p_date,'yyyymmdd'),p_step),'yyyymm')||v_rtnValue;
return v_rtnValue;
end my_add_months;
select td,tm,(select qty from aaa where thedate=tm||td) qty,
ntm,(select qty from aaa where thedate=ntm||td) nqty from
(select ltrim(rtrim(to_char(rownum,'00'))) td from aaa where rownum <= 31 ) c,
(select to_char(trunc(to_date(thedate,'yyyymmdd'),'mm'),'yyyymm') tm,
to_char(trunc(add_months(to_date(thedate,'yyyymmdd'),-1),'mm'),'yyyymm') ntm
from aaa
group by trunc(to_date(thedate,'yyyymmdd'),'mm'),
trunc(add_months(to_date(thedate,'yyyymmdd'),-1),'mm')) d
order by tm,td
/
select max(date_sal),min(date_sal) from
(select col_date,sal,to_char(col_date,'yyyymmdd')||' '||sal date_sal,row_number() over(pratition by trunc(col_date,'mm') order by col_date) rm from tabl where col_date between trunc(add_months(date_value,-1),'mm') and last_day(date_value))
group by rm order by rm
select a.thedate,a.qty,b.thedate,b.qty
from tab a,tab b,(select rownum rn from tab where rownum <= 31) c
where a.thedate like '200307%'
and b.thedate like '200306%'
and c.rn = substr(a.thedate,7)(+)
and c.rn = substr(b.thedate,7)(+)
select a.thedate,a.qty,b.thedate,b.qty
from tab a,tab b,(select rownum rn from tab where rownum <= 31) c
where a.thedate like '200307%'
and a.thedate like '200306%'
and c.rn = substr(a.thedate,7)(+)
and c.rn = substr(b.thedate,7)(+)