您的位置:首页 > 其它

一个存储过程实例

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 ;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: