今天遇到一个问题,需要查询两个月内的数据,但是SQL语句没有办法实现,就自己写了一个简单的存储过程以此来实现自己需要的功能。 使用的PL/SQL工具,实现存储过程中数据的输出。 1、首先建立一个“test window”(即测试窗口),然后在里面写代码。 2、然后
今天遇到一个问题,需要查询两个月内的数据,但是SQL语句没有办法实现,就自己写了一个简单的存储过程以此来实现自己需要的功能。
使用的PL/SQL工具,实现存储过程中数据的输出。
1、首先建立一个“test window”(即测试窗口),然后在里面写代码。
2、然后写上自己的代码,我的如下:
自己也可以根据需要设置为可传入参数的功能
declare
v_date date := to_date('2014-11-01', 'yyyy-mm-dd');
e_date date := to_date('2014-11-30', 'yyyy-mm-dd');
v_cur sys_refcursor;
v_pay number;
v_source varchar2(20);
v_new number;
v_invest number;
p_date date;
begin
while (v_date <= e_date) loop
open v_cur for
select v_date,
aa.source,
nvl(aa.pay_money, 0),
nvl(bb.new_open_nums, 0),
nvl(cc.invest, 0)
from (select sum(order_money_rmb) as pay_money, source
from (select distinct order_id, order_money_rmb, source
from pps_adsmd_wt_report_order t
where t.addtime >= v_date
and t.addtime < v_date + 1
and t.order_kind >= 1
and t.order_kind <= 30
and t.user_type = 1)
group by source) aa
left join (select sum(bb.new_open_nums) as new_open_nums, bb.source
from pps_adsmd_wt_report bb
where bb.report_date >= v_date
and bb.report_date < v_date + 1
group by bb.source) bb
on aa.source = bb.source
left join (select sum(t1.cost_money) as invest, source_name
from wt_adsmd_source_cost t1
where t1.start_time = v_date
group by source_name) cc
on aa.source = cc.source_name;
v_date := v_date + 1;
loop
fetch v_cur
into p_date, v_source, v_pay, v_new, v_invest;
exit when v_cur%notfound;
DBMS_OUTPUT.PUT_LINE(p_date || ' ' || v_source || ' ' || v_pay || ' ' ||
v_new || ' ' || v_invest);
end loop;
end loop;
end;
3、运行SQL,在输出中即可看到结果。
以此记录,仅供个人查看。