美文网首页
写的第一个存储过程

写的第一个存储过程

作者: 圈半球 | 来源:发表于2019-01-03 18:47 被阅读0次

BEGIN

  DELETE FROM DM.TM_JZYX_SY_ZYPPSYXS_GX_TMP;

insert INTO  DM.TM_JZYX_SY_ZYPPSYXS_GX_TMP(NY,SJDM,SJDMJC,XMID,XMMC,SYXL,XSJE,DXJG) 

with tmp as (SELECT NY,SJDM,SJDMJC,PPBS xmid,PPMC xmmc,JLBS,SYXL,XSJE,GGBS FROM DM.TM_JZYX_QGSYXLMX),

tmp1 as (SELECT GGDM,PFJ,KSRQ,JSRQ FROM EDW.TC_JGML WHERE PFJ >= 400 AND JGBS = '元/条'),

tmp4 as (SELECT NY,SJDM,SJDMJC,concat(PPBS,right(ggbs,3)) xmid,concat(PPMC,SUBSTR(ggmc,5)) xmmc,JLBS,SYXL,XSJE,GGBS FROM DM.TM_JZYX_QGSYXLMX WHERE PPMC='真龙'),

tmp2 AS

(select NY,SJDM,SJDMJC,'1154_123' xmid,'其中:三类烟以上' xmmc,sum(case when SYXL is null then 0 else syxl end) syxl,sum(case when XSJE is null then 0 else xsje end) xsje

from tmp

where xmmc='真龙'and (JLBS = '1' Or JLBS = '2' Or JLBS = '3')

group by NY,SJDM,SJDMJC,xmid,xmmc

  union all

(select NY,SJDM,SJDMJC,xmid,xmmc,sum(case when SYXL is null then 0 else syxl end) syxl,sum(case when XSJE is null then 0 else xsje end) xsje

from tmp4

group by NY,SJDM,SJDMJC,xmid,xmmc)

  union all

(select NY,'000'SJDM,'全国'SJDMJC,xmid,xmmc,sum(case when SYXL is null then 0 else syxl end) syxl,sum(case when XSJE is null then 0 else xsje end) xsje

from tmp4

group by NY,SJDM,SJDMJC,xmid,xmmc)

  union all

(select NY,'000'SJDM,'全国'SJDMJC,'1154_123'xmid,'其中:三类烟以上'xmmc,sum(case when SYXL is null then 0 else syxl end) syxl,sum(case when XSJE is null then 0 else xsje end) xsje

from tmp

where xmmc='真龙'AND (JLBS = '1' Or JLBS = '2' Or JLBS = '3')

group by NY,SJDM,SJDMJC,xmid,xmmc)

union all

(select a.ny ny,a.sjdm sjdm,a.SJDMJC SJDMJC,a.xmid xmid,a.xmmc xmmc,sum(case when a.syxl is null then 0 else a.syxl end)syxl,sum(case when a.xsje is null then 0 else a.xsje end) xsje from

(select NY,SJDM,SJDMJC,'1154_400' xmid, '其中:400元/条以上' xmmc, syxl,xsje,

ggbs,concat(ny,'-01') ny2 from tmp

where xmmc='真龙')a

inner join tmp1 b on a.ggbs = b.ggdm

where a.NY2 >= b.KSRQ AND a.NY2 <= b.JSRQ

group by a.ny,a.sjdm,a.SJDMJC,a.xmid,a.xmmc)

union all

(select a.ny ny,a.sjdm sjdm,a.SJDMJC SJDMJC,a.xmid xmid,a.xmmc xmmc,sum(case when a.syxl is null then 0 else a.syxl end)syxl,sum(case when a.xsje is null then 0 else a.xsje end) xsje from

(select NY,'000' SJDM,'全国' SJDMJC,'1154_400' xmid, '其中:400元/条以上' xmmc, syxl,xsje,

ggbs,concat(ny,'-01') ny2 from tmp

where xmmc='真龙')a

inner join tmp1 b on a.ggbs = b.ggdm

where a.NY2 >= b.KSRQ AND a.NY2 <= b.JSRQ

group by a.ny,a.sjdm,a.SJDMJC,a.xmid,a.xmmc)

),

tmp3 as (SELECT

NY,SJDM,SJDMJC, xmid,xmmc,sum(case when SYXL is null then 0 else syxl end) syxl,sum(case when XSJE is null then 0 else xsje end) xsje from tmp2

group by NY,SJDM,SJDMJC, xmid,xmmc)

select case when ny is null then '' else ny end ny,case when sjdm is null then '' else sjdm end sjdm,SJDMJC,case when xmid is null then '' else xmid end xmid,xmmc,syxl,xsje,

case when SYXL <> 0 then  XSJE*1.0000/(SYXL*10000) else null end dxjg from TMP3

where sjdm is not null;

END;

相关文章

  • 写的第一个存储过程

    BEGIN DELETE FROM DM.TM_JZYX_SY_ZYPPSYXS_GX_TMP; insert ...

  • Oracle存储过程总结

    Oracle存储过程总结 1.存储过程结构 1.1 第一个存储过程 上面就是一个最简单的存储过程。一个存储过程大体...

  • Oracle存储过程基本结构说明

    1.存储过程结构 1.1第一个存储过程 上面是一个简单的存储过程实例.一个存储过程大体分为这么几个部分: 存储过程...

  • 存储过程

    1.第一个存储过程,打印hello word/*调用存储过程 exec sayhello(); beginsayh...

  • 项目中Orcale存储过程优化

    今天对之前写的Orcale存储过程做了一些优化,使其变得更加灵活,之前写的存储过程是使用游标存储SQL执行结果,但...

  • mysql定期删除数据

    整个过程总共分为三步: 写存储过程 写Event事件 把Event事件设置成Enable并打开事件任务 存储过程 ...

  • 开发日记:Orcale 存储过程(一)

    由于项目中用到存储过程,这两天把存储过程方面的知识简单回顾了一下并分享给大家。 编写第一个存储过程 上述代码实现的...

  • Sql Server 中关于@@ERROR的一个小小误区

    我们经常写存储过程的时候会用到@@ERROR来判断执行是否成功,很久没有写复杂点的存储过程了,今天发现前段时间写的...

  • 第一个存储过程

    车轮战项目 ,老板让我写个定时计算每日答题的总人数的存储过程,有点蒙逼,没写过存储过程 一步步来 1.首先先写一个...

  • Mysql存储过程

    阅读目录:MySQL存储过程_创建-调用-参数 存储过程:SQL中的“脚本” 创建存储过程 调用存储过程 存储过程...

网友评论

      本文标题:写的第一个存储过程

      本文链接:https://www.haomeiwen.com/subject/ffyqrqtx.html