一个存储过程实例
2010-04-21 16:32
393 查看
这个是今天写的一个存储过程的实例
其实已经写了3天了,但是由于之前只写过helloworld级别的存储过程 所以一直没有进展。。。
这个也是根据一个同事写的demo写的 大约用了半个多小时吧。。。。
其中对比自己之前写的 发现一个关键的地方就是union
之前一直想一次把3种费用的数据都统计出来 结果很难实现
而用union的话 可以用3条记录统计3种费用 再在外层sum一下就可以了~~~~~~~~
DELIMITER $$
DROP PROCEDURE IF EXISTS yyfyzb_pro_1_d $$
CREATE PROCEDURE yyfyzb_pro_1_d (IN startDate DATE,IN endDate DATE)
BEGIN
declare d_current_period DATE;
declare d_previous_period DATE ;
set d_current_period = startDate;
set d_previous_period = DATE_SUB(startDate , interval 1 year);
WHILE (d_current_period < endDate) DO
insert into yyfyzb(YBLB,ZB_DQ,ZB_SQ,ZB_TBZF,MJZFY_DQ,MJZFY_SQ,MJZFY_TBZF,MZTBFY_DQ,MZTBFY_SQ,MZTBFY_TBZF,ZYFY_DQ,ZYFY_SQ,ZYFY_TBZF,TRANS_DATE,DIMENSION_TYPE,DIMENSION,SJPD,SJPDZ)
select h.ybbf,
h.total_cur_fee,
h.total_pre_fee,
if(total_pre_fee=0, NULL, ((total_cur_fee-total_pre_fee)/total_pre_fee)*100) total_tbzf,
h.mjz_cur_fee,
h.mjz_pre_fee,
if(mjz_pre_fee=0, NULL, ((mjz_cur_fee-mjz_pre_fee)/mjz_pre_fee)*100) mjz_tbzf,
h.mztb_cur_fee,
h.mztb_pre_fee,
if(mztb_pre_fee=0, NULL, ((mztb_cur_fee-mztb_pre_fee)/mztb_pre_fee)*100) mztb_tbzf,
h.zy_cur_fee,
h.zy_pre_fee,
if(zy_pre_fee=0, NULL, ((zy_cur_fee-zy_pre_fee)/zy_pre_fee)*100) zy_tbzf,
d_current_period,
'1', /*维度类型,在此1表示人员分布*/
h.rylb_dm, /*维度代码,对于城镇居民,该值为'4',对于城镇职工该值是1或2或3*/
'D', /*时间频度类型*/
d_current_period /*时间频度值*/
from (
select g.ybbf,
g.rylb_dm,
sum(g.mjz_cur_fee + g.mztb_cur_fee + g.zy_cur_fee) total_cur_fee,
sum(g.mjz_pre_fee + g.mztb_pre_fee + g.zy_pre_fee) total_pre_fee,
sum(g.mjz_cur_fee) mjz_cur_fee,
sum(g.mjz_pre_fee) mjz_pre_fee,
sum(g.mztb_cur_fee) mztb_cur_fee,
sum(g.mztb_pre_fee) mztb_pre_fee,
sum(g.zy_cur_fee) zy_cur_fee,
sum(g.zy_pre_fee) zy_pre_fee
from
(
/*mjz fee*/
(select m.YBBF,
if (m.YBBF = '1', '4', c.rylb_dm) rylb_dm,
sum(m.XYF + m.Z_CHENG_YF + m.Z_CAO_YF) mjz_cur_fee,
ifnull(
(select t.MJZFY_DQ from yyfyzb t
where t.TRANS_DATE = d_previous_period and
t.DIMENSION_TYPE = '1' and
t.DIMENSION = if (m.YBBF = '1', '4', c.rylb_dm) and
m.YBBF = t.yblb and
t.sjpd='D')
,0) mjz_pre_fee,
0 mztb_cur_fee,
0 mztb_pre_fee,
0 zy_cur_fee,
0 zy_pre_fee
from mjzzf m,patient_category c
where m.kh = c.kh and
c.qsrq <= d_current_period and
(c.zzrq is null or c.zzrq >= d_current_period) and
m.JYRQ >= CONCAT(d_current_period, ' 00:00:00') and
m.JYRQ <= CONCAT(d_current_period, ' 23:59:59')
group by m.YBBF,if (m.YBBF = '1', '4', c.rylb_dm)
)
union all
/*mztb fee*/
(select d.YBBF,
if (d.YBBF = '1', '4', c.rylb_dm) rylb_dm,
0 mjz_cur_fee,
0 mjz_pre_fee,
sum(d.XYF + d.Z_CHENG_YF + d.Z_CAO_YF) mztb_cur_fee,
ifnull((select t.MZTBFY_DQ from yyfyzb t
where t.TRANS_DATE = d_previous_period and
t.DIMENSION_TYPE = '1' and
t.DIMENSION = if (d.YBBF = '1', '4', c.rylb_dm) and
d.YBBF = t.yblb and
t.sjpd='D')
,0) mztb_pre_fee,
0 zy_cur_fee,
0 zy_pre_fee
from dbzf d,patient_category c
where d.kh = c.kh and
c.qsrq <= d_current_period and
(c.zzrq is null or c.zzrq >= d_current_period) and
d.JYRQ >= CONCAT(d_current_period, ' 00:00:00') and
d.JYRQ <= CONCAT(d_current_period, ' 23:59:59')
group by d.YBBF,if (d.YBBF = '1', '4', c.rylb_dm)
)
union all
/*zy fee*/
(select z.YBBF,
if (z.YBBF = '1', '4', c.rylb_dm) rylb_dm,
0 mjz_cur_fee,
0 mjz_pre_fee,
0 mztb_cur_fee,
0 mztb_pre_fee,
sum(z.XYF + z.Z_CHENG_YF + z.Z_CAO_YF) zy_cur_fee,
ifnull((select t.ZYFY_DQ from yyfyzb t
where t.TRANS_DATE = d_previous_period and
t.DIMENSION_TYPE = '1' and
t.DIMENSION = if (z.YBBF = '1', '4', c.rylb_dm) and
z.YBBF = t.yblb and
t.sjpd='D')
,0) zy_pre_fee
from zyzf z,
jgzy_cryk k,
patient_category c
where z.YLJG_DM = k.YLJG_DM and
z.LSH_OLD = k.LSH_OLD and
k.CYBZ = '1' and
z.kh = c.kh and
c.qsrq <= d_current_period and
(c.zzrq is null or c.zzrq >= d_current_period) and
z.JYRQ >= CONCAT(d_current_period, ' 00:00:00') and
z.JYRQ <= CONCAT(d_current_period, ' 23:59:59')
group by z.YBBF,if (z.YBBF = '1', '4', c.rylb_dm)
)
)g
group by g.ybbf, g.rylb_dm
)h;
SET d_current_period = DATE_ADD(d_current_period,INTERVAL 1 DAY);
SET d_previous_period = DATE_ADD(d_previous_period,INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER ;
其实已经写了3天了,但是由于之前只写过helloworld级别的存储过程 所以一直没有进展。。。
这个也是根据一个同事写的demo写的 大约用了半个多小时吧。。。。
其中对比自己之前写的 发现一个关键的地方就是union
之前一直想一次把3种费用的数据都统计出来 结果很难实现
而用union的话 可以用3条记录统计3种费用 再在外层sum一下就可以了~~~~~~~~
DELIMITER $$
DROP PROCEDURE IF EXISTS yyfyzb_pro_1_d $$
CREATE PROCEDURE yyfyzb_pro_1_d (IN startDate DATE,IN endDate DATE)
BEGIN
declare d_current_period DATE;
declare d_previous_period DATE ;
set d_current_period = startDate;
set d_previous_period = DATE_SUB(startDate , interval 1 year);
WHILE (d_current_period < endDate) DO
insert into yyfyzb(YBLB,ZB_DQ,ZB_SQ,ZB_TBZF,MJZFY_DQ,MJZFY_SQ,MJZFY_TBZF,MZTBFY_DQ,MZTBFY_SQ,MZTBFY_TBZF,ZYFY_DQ,ZYFY_SQ,ZYFY_TBZF,TRANS_DATE,DIMENSION_TYPE,DIMENSION,SJPD,SJPDZ)
select h.ybbf,
h.total_cur_fee,
h.total_pre_fee,
if(total_pre_fee=0, NULL, ((total_cur_fee-total_pre_fee)/total_pre_fee)*100) total_tbzf,
h.mjz_cur_fee,
h.mjz_pre_fee,
if(mjz_pre_fee=0, NULL, ((mjz_cur_fee-mjz_pre_fee)/mjz_pre_fee)*100) mjz_tbzf,
h.mztb_cur_fee,
h.mztb_pre_fee,
if(mztb_pre_fee=0, NULL, ((mztb_cur_fee-mztb_pre_fee)/mztb_pre_fee)*100) mztb_tbzf,
h.zy_cur_fee,
h.zy_pre_fee,
if(zy_pre_fee=0, NULL, ((zy_cur_fee-zy_pre_fee)/zy_pre_fee)*100) zy_tbzf,
d_current_period,
'1', /*维度类型,在此1表示人员分布*/
h.rylb_dm, /*维度代码,对于城镇居民,该值为'4',对于城镇职工该值是1或2或3*/
'D', /*时间频度类型*/
d_current_period /*时间频度值*/
from (
select g.ybbf,
g.rylb_dm,
sum(g.mjz_cur_fee + g.mztb_cur_fee + g.zy_cur_fee) total_cur_fee,
sum(g.mjz_pre_fee + g.mztb_pre_fee + g.zy_pre_fee) total_pre_fee,
sum(g.mjz_cur_fee) mjz_cur_fee,
sum(g.mjz_pre_fee) mjz_pre_fee,
sum(g.mztb_cur_fee) mztb_cur_fee,
sum(g.mztb_pre_fee) mztb_pre_fee,
sum(g.zy_cur_fee) zy_cur_fee,
sum(g.zy_pre_fee) zy_pre_fee
from
(
/*mjz fee*/
(select m.YBBF,
if (m.YBBF = '1', '4', c.rylb_dm) rylb_dm,
sum(m.XYF + m.Z_CHENG_YF + m.Z_CAO_YF) mjz_cur_fee,
ifnull(
(select t.MJZFY_DQ from yyfyzb t
where t.TRANS_DATE = d_previous_period and
t.DIMENSION_TYPE = '1' and
t.DIMENSION = if (m.YBBF = '1', '4', c.rylb_dm) and
m.YBBF = t.yblb and
t.sjpd='D')
,0) mjz_pre_fee,
0 mztb_cur_fee,
0 mztb_pre_fee,
0 zy_cur_fee,
0 zy_pre_fee
from mjzzf m,patient_category c
where m.kh = c.kh and
c.qsrq <= d_current_period and
(c.zzrq is null or c.zzrq >= d_current_period) and
m.JYRQ >= CONCAT(d_current_period, ' 00:00:00') and
m.JYRQ <= CONCAT(d_current_period, ' 23:59:59')
group by m.YBBF,if (m.YBBF = '1', '4', c.rylb_dm)
)
union all
/*mztb fee*/
(select d.YBBF,
if (d.YBBF = '1', '4', c.rylb_dm) rylb_dm,
0 mjz_cur_fee,
0 mjz_pre_fee,
sum(d.XYF + d.Z_CHENG_YF + d.Z_CAO_YF) mztb_cur_fee,
ifnull((select t.MZTBFY_DQ from yyfyzb t
where t.TRANS_DATE = d_previous_period and
t.DIMENSION_TYPE = '1' and
t.DIMENSION = if (d.YBBF = '1', '4', c.rylb_dm) and
d.YBBF = t.yblb and
t.sjpd='D')
,0) mztb_pre_fee,
0 zy_cur_fee,
0 zy_pre_fee
from dbzf d,patient_category c
where d.kh = c.kh and
c.qsrq <= d_current_period and
(c.zzrq is null or c.zzrq >= d_current_period) and
d.JYRQ >= CONCAT(d_current_period, ' 00:00:00') and
d.JYRQ <= CONCAT(d_current_period, ' 23:59:59')
group by d.YBBF,if (d.YBBF = '1', '4', c.rylb_dm)
)
union all
/*zy fee*/
(select z.YBBF,
if (z.YBBF = '1', '4', c.rylb_dm) rylb_dm,
0 mjz_cur_fee,
0 mjz_pre_fee,
0 mztb_cur_fee,
0 mztb_pre_fee,
sum(z.XYF + z.Z_CHENG_YF + z.Z_CAO_YF) zy_cur_fee,
ifnull((select t.ZYFY_DQ from yyfyzb t
where t.TRANS_DATE = d_previous_period and
t.DIMENSION_TYPE = '1' and
t.DIMENSION = if (z.YBBF = '1', '4', c.rylb_dm) and
z.YBBF = t.yblb and
t.sjpd='D')
,0) zy_pre_fee
from zyzf z,
jgzy_cryk k,
patient_category c
where z.YLJG_DM = k.YLJG_DM and
z.LSH_OLD = k.LSH_OLD and
k.CYBZ = '1' and
z.kh = c.kh and
c.qsrq <= d_current_period and
(c.zzrq is null or c.zzrq >= d_current_period) and
z.JYRQ >= CONCAT(d_current_period, ' 00:00:00') and
z.JYRQ <= CONCAT(d_current_period, ' 23:59:59')
group by z.YBBF,if (z.YBBF = '1', '4', c.rylb_dm)
)
)g
group by g.ybbf, g.rylb_dm
)h;
SET d_current_period = DATE_ADD(d_current_period,INTERVAL 1 DAY);
SET d_previous_period = DATE_ADD(d_previous_period,INTERVAL 1 DAY);
END WHILE;
END $$
DELIMITER ;
相关文章推荐
- 保存一个package的存储过程的实例
- 写一个简单的存储过程实例的笔记
- sql plus创建一个简单的存储过程实例
- mysql存储过程的一个完整实例
- .若A,B两个实体是一对多的包含关系。利用存储过程,实现数据访问层层的一个实例。
- SQL SERVER 2000 连接ORCLE数据库 (加了一个存储过程的实例)
- sql的存储过程实例--动态根据表数据复制一个表的数据到另一个表
- SQL Server一个实例中调用另外一个实例中的存储过程
- SQL Server一个实例中调用另外一个实例中的存储过程
- 一个简明的ERP存储过程实例
- 一个使用游标的存储过程的实例
- MSSQL数据库:存储过程实例学习(1)从两个表中取出头两行,然后合并到一个表中
- 存储过程的一个简单实例
- MYSQL存储过程的一个完整实例
- 简单的存储过程实例2--对比三个表中的数据,将三个表中金额挑选一个最优值
- 关于一个简单的mysql存储过程实例
- 如何得到任意一个存储过程的参数列表的
- Oracle 游标及存储过程实例
- 把多条记录转为一个字符串输出存储过程
- 一个存储过程和shell操作mysql