下面是一个用于计算,每个月从4月起各个月的记录条数统计,直到当前月
1.打开PLSQL,新建一个存储过程,点击菜单文件|新建|程序窗口|过程,在弹出的对话框中输入存储过程名字,确定
复制下面的代码到编辑器,运行
create or replace procedure PCREPORT is
startDate DATE; --起始如期
nowTime DATE; --当前日期
nowTime2 DATE; --当前日期下限用于支持跨年
orderDate DATE; --计算时候的当前下订单日期
orderDate2 DATE; --跨年使用的日期范围下限
returns number; --退订总数
renew number; --续费总数
active number; --激活人数
noActive number; --未激活人数
activeReturn number; --激活退订人数
noActiveReturn number; --未激活退订人数
reportDateActive number; --报表时间激活的人数
tempStr varchar2(3000);
i number;
number_value number;
--1.起始时间与当前时间减法,用结果做循环遍历的次数
--2.每次遍历在起始时间上加,遍历次数的月值
--3.将这个时间值作为条件,查询统计出一个统计值,将这个值插入到数据库对应的字段
begin
startDate := to_date('2009-04-01', 'yyyy-mm-dd');
/* SELECT to_char(sysdate, 'yyyy-mm-dd ') into tempStr from dual;
nowTime := to_date(tempStr, 'yyyy-mm-dd');*/
select to_date(to_char(sysdate, 'YYYYMM') || '01', 'YYYY-MM-DD')
into nowTime
from dual;
nowTime2 := add_months(nowTime, 1);
number_value := months_between(nowTime, startDate); --月份差
i := 0;
for i in 0 .. number_value loop
orderDate := add_months(startDate, i);
orderDate2 := add_months(orderDate, 1);
-- 续费及激活总数的计算
select count(*)
into renew
from ord_order
where AGREE_TO_TIME between orderDate and orderDate2 --(select to_char(AGREE_TO_TIME, 'mm') from dual) =
and (end_time = to_date('1900-01-01', 'yyyy-mm-dd') or
end_time >= nowTime); --(select to_char(end_time, 'mm') from dual) >= '07');
--续费用户退订总人数
select count(*)
into returns
from ord_order
where AGREE_TO_TIME between orderDate and orderDate2 --(select to_char(AGREE_TO_TIME, 'mm') from dual) = '05'
and end_time between nowTime and nowTime2; --(select to_char(end_time, 'mm') from dual) = '07';
--续费用户激活未退订
select count(*)
into noActiveReturn
from ord_order
where AGREE_TO_TIME between orderDate and orderDate2 -- (select to_char(AGREE_TO_TIME, 'mm') from dual) = '06'
and end_time between nowTime and nowTime2 --(select to_char(end_time, 'mm') from dual) = '07'
and BECOME_EFFECTIVE_TIME < nowTime --to_date('2009-07-01', 'yyyy-mm-dd')
and BECOME_EFFECTIVE_TIME = to_date('1900-01-01', 'yyyy-mm-dd');
-- 当前报表月在个月激活的人数
select count(*)
into reportDateActive
from ord_order
where AGREE_TO_TIME between orderDate and orderDate2 --(select to_char(AGREE_TO_TIME, 'mm') from dual) = '05'
and (end_time = to_date('1900-01-01', 'yyyy-mm-dd') or
end_time >= nowTime) --(select to_char(end_time, 'mm') from dual) >= '06')
and BECOME_EFFECTIVE_TIME between nowTime and nowTime2; --(select to_char(BECOME_EFFECTIVE_TIME, 'mm') from dual) = '06';
-----------------------------之前经过校验得到正确结果---------------------
--续费用户报表月激活人数
select count(*)
into active
from ord_order
where AGREE_TO_TIME between orderDate and orderDate2 --(select to_char(AGREE_TO_TIME, 'mm') from dual) = '04'
and BECOME_EFFECTIVE_TIME != to_date('1900-01-01', 'yyyy-mm-dd')
and BECOME_EFFECTIVE_TIME < nowTime --(select to_char(BECOME_EFFECTIVE_TIME, 'mm') from dual) < '07'
and ((end_time = to_date('1900-01-01', 'yyyy-mm-dd')) or
end_time >= nowTime); --(select to_char(end_time, 'mm') from dual) >= '07');
activeReturn := returns - noActiveReturn;
noActive := renew - active - reportDateActive;
--做插入操作
insert into report_pc51
(REPORTDATE,
ORDERDATE,
RENEW,
RETURNS,
NOACTIVERETURN,
REPORTDATEACTIVE,
ACTIVERETURN,
ACTIVE,
NOACTIVE)
values
(nowTime,
orderDate,
renew,
returns,
noActiveReturn,
reportDateActive,
activeReturn,
active,
noActive);
dbms_output.put_line(i);
end loop;
COMMIT;
end PCREPORT;
分享到:
相关推荐
oracle 存储过程示例oracle 存储过程示例oracle 存储过程示例oracle 存储过程示例oracle 存储过程示例oracle 存储过程示例oracle 存储过程示例oracle 存储过程示例
这是一个简单的oracle代码,可进行参考
本文实例讲述了Python使用cx_Oracle调用Oracle存储过程的方法。分享给大家供大家参考,具体如下: 这里主要测试在Python中通过cx_Oracle调用PL/SQL。 首先,在数据库端创建简单的存储过程。 create or replace ...
oracle的一个简单存储过程实例,有原文件和使用说明。
oracle存储过程学习经典(实例),适合刚学习存储过程的广大程序员们
Oracle存储过程基本语法及示例,都是些基础,我喜欢基础扎实
该文档详细讲解Oracle在存储过程中如何利用Out参数返回一个结果集。示例简单易懂。
java调用oracle存储过程的几个小示例+分页
一些sql server和oracle存储过程示例
Orcale存储过程示例
oracle 的函数、存储过程、游标、简单实例 oracle常用语句oracle 的函数、存储过程、游标、简单实例 oracle常用语句
NULL 博文链接:https://quarterlifeforjava.iteye.com/blog/2124048
NULL 博文链接:https://jsufly.iteye.com/blog/646683
这是利用SSH2连接oracle数据库,利用存储过程运行,分层结构,有测试与页码操作,一般提供的源码不能直接运行,这个进行了修改,这种运行方法,网上很少提及,可修改为作者自己的数据库进行测试。
asp.net实现oracle存储过程增删改查示例 (1)三层架构实现 (2)oracle存储过程实现增删改查 (3)oracle sql语句实现增删改查 (4)asp.net代码调用 (5)ibatis框实现增删改查 我在公司学习了一个星期的作品,...
java调用Oracle存储过程的简单源码示例, 包括无返回参数,返回单个参数和返回参数集合三种调用情况!
oracle数据库同步perl脚本示例 其中有2个文件,一个是 perl同步脚本,另一个是存储过程; 大致思路是:perl脚本从数据库A同步数据至库B的临时表中,再通过存储过程将库B中的临时数据过滤导入正式数据当中。
Oracle分页存储过程,根据指定条件分页 并附上使用示例(C#代码) 注:不能查询视图或者包含distinct、group by、inner join的子句
一些学习sql语句的例句。包括存储过程、游标、自定义函数等各种高难度sql语句写法。