遇到一个需求统计历史每个月底的数据插入到表中,查询了资料发现使用游标会很方便,记录一下解决思路
select to_char(lastday, 'yyyy-mm-dd') lastday
from (select last_day(add_months(to_date('201408', 'yyyymm'), level)) lastday
from dual
connect by level <= 50)
order by lastday desc;
declare --声明两个变量
v_date VARCHAR2(50);
cursor yb is
select to_char(lastday,'yyyy-mm-dd') lastday from (
select last_day(add_months(to_date('201408','yyyymm'),level)) lastday from dual connect by level<=50
) order by lastday desc;
begin
open yb; --打开游标
loop --开始标记
fetch yb into v_date;--游标赋值 当然这边可以赋值多个值
exit when yb%notfound;--游标一条一条地遍历记录,当找不到记录时退出
begin
insert into business_info
(id,
LOANAMOUNT,
LOANCOUNT,
LOANBALANCE,
LOANBALANCENUM,
INTEREST,
BIDMEMCOUNT,
LOANMEMCOUNT,
BIDMEMNUM,
LOANMEMNUM,
TENPER,
MAXPER,
counttime)
select business_info_id.nextval id, a.*,b.*,c.*,d.*,e.*,f.*,g.*,h.*,i.*,to_timestamp(v_date,'yyyy-mm-dd hh24:mi:ss') as counttime from
(
SELECT nvl(sum(loanamount), 0) as loanamount, count(1) loancount
FROM p_loan
WHERE status IN (06, 07)
AND loantype = 0
and to_char(createtime,'yyyy-mm-dd')<=v_date
)a,
(
select nvl(sum(rprincipal), 0) as yumoney
from repayment
where status in ('0', '1')
and isflag = '1'
and to_char(createtime,'yyyy-mm-dd')<=v_date
)b,
(
select count(1) as zdbs
from p_loan
where loantype = '0'
and status = '06'
and to_char(createtime, 'yyyy-mm-dd') <= v_date
)c,
(
Select Nvl(Sum(Money),'0') as yulixi--利息余额
From (Select Rp.Bidmemberid,
Rp.Cloanid,
Sum(Nvl(Money, 0) + Nvl(Money1, '0')) As Money
From (Select Sum(a.Srinterest) As Money, a.Bidmemberid, a.Cloanid
From Repayment a
Where a.Status In ('0', '1')
And a.Isflag = '1'
and to_char(createtime,'yyyy-mm-dd')<=v_date
Group By a.Bidmemberid, a.Cloanid) Rp
Left Join (Select Nvl(Sum(b.Sramount), 0) As Money1,
b.Cloanid,
b.Memberid
From Repaymentcost b
Where b.Status In ('0', '1')
And b.Isflag = '1'
and to_char(createtime,'yyyy-mm-dd')<=v_date
Group By b.Cloanid, b.Memberid) Rt
On Rp.Cloanid = Rt.Cloanid
And Rp.Bidmemberid = Rt.Memberid
Group By Rp.Bidmemberid, Rp.Cloanid)
)d,
(
Select Count(Distinct memberid) As bidmemcount
from p_bid
Where status in ('2', '3')
and to_char(createtime, 'yyyy-mm-dd') <=v_date
)e,
(
select count(distinct(memberid)) as loanmemcount
from p_loan
where status in ('06', '07')
and loantype = '0'
and to_char(createtime,'yyyy-mm-dd')<=v_date
)f,
(
Select Count(Distinct Bidmemberid) As Tzcount,
Count(Distinct Memberid) As Jkcount
From Repayment
Where Isflag = '1'
And Status = '0'
and to_char(createtime,'yyyy-mm-dd')<=v_date
)g,
(
select sum(rate) as tenrate
from (select decode(b.ramount,0,0,trunc((a.loanamount / b.ramount), 4) * 100) as rate --占比
from (select m.enrolname, sum(r.rprincipal) as loanamount
from repayment r
left join member m
on r.memberid = m.memid
where r.isflag = '1'
and to_char(r.createtime,'yyyy-mm-dd')<=v_date
group by m.enrolname
order by loanamount desc) a,
(select nvl(sum(t.rprincipal),'0') as ramount
from repayment t
where isflag = '1'
and to_char(createtime,'yyyy-mm-dd')<=v_date) b)
where rownum <= 10
)h,
(
select sum(rate) as tenrate
from (select decode(b.ramount,0,0,trunc((a.loanamount / b.ramount), 4) * 100) as rate --占比
from (select m.enrolname, sum(r.rprincipal) as loanamount
from repayment r
left join member m
on r.memberid = m.memid
where r.isflag = '1'
and to_char(r.createtime,'yyyy-mm-dd')<=v_date
group by m.enrolname
order by loanamount desc) a,
(select nvl(sum(t.rprincipal),'0') as ramount
from repayment t
where isflag = '1'
and to_char(createtime,'yyyy-mm-dd')<=v_date) b)
where rownum <= 1
)i;
exception --异常抛出
when others then
dbms_output.put_line(v_date);
end;
end loop; --结束标记
commit;
close yb; --关闭游标
end; --结束
/ --这个斜杠用处很大,比如好多条存储过程的话,可以写在后面一起执行。