sql求同比

select

        to_char(t.CREDATE,'mm') as CREDATE,

  t.ENTRYID,

  t2.ENTRYID,

  t2.ENTRYID,

        nvl(t1.TOTAL,0) as "同期TOTAL",--同期

        nvl(t1.SETTLEQTY,0) as "同期SETTLEQTY",--同期

        nvl(t2.TOTAL ,0)as "去年TOTAL",--去年

        nvl(t2.SETTLEQTY,0) as "去年SETTLEQTY",--去年

        to_char(t.CREDATE,'yyyy-mm') as YEARN,

        to_char(t.CREDATE,'yyyy') as YEAR,

        nvl(sum(t.TOTAL),0) as TOTAL,--当月

        nvl(sum(t.SETTLEQTY),0) as SETTLEQTY--当月

from BMS_SA_DTLQRY_V t

left join (

select  ENTRYID,

        to_char(CREDATE,'yyyymm') as year_mon,

        sum(TOTAL) as TOTAL,

        sum(SETTLEQTY) as SETTLEQTY

from BMS_SA_DTLQRY_V

group by to_char(CREDATE,'yyyymm'),ENTRYID

)t1

        on to_char(add_months(t.CREDATE,-12),'yyyymm')=t1.year_mon

left join (

        select ENTRYID,

                to_char(CREDATE,'yyyy') as yearn,

                sum(TOTAL) as TOTAL,

                sum(SETTLEQTY) as SETTLEQTY

from BMS_SA_DTLQRY_V

group by to_char(CREDATE,'yyyy'),ENTRYID

)t2

     on  to_char(add_months(t.CREDATE,-12),'yyyy')=t2.yearn

 

(select DECODE(sum(t.TOTAL), 0,SUM(t2.TOTAL),0, ()) from BMS_SA_DTLQRY_V )

group by to_char(CREDATE,'mm'),

        to_char(t.CREDATE,'yyyy-mm'),

        to_char(CREDATE,'yyyy'),

        t1.TOTAL,

t.ENTRYID,

        t1.SETTLEQTY,

        t2.TOTAL,

        t2.SETTLEQTY,

t2.ENTRYID,

  t2.ENTRYID 

ZAW 发布于 2021-6-4 16:24
1min目标场景问卷 立即参与
回答问题
悬赏:6 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
1
liu.yangLv2初级互助
发布于2021-6-4 16:32(编辑于 2021-6-4 16:41)

select        to_char(t.CREDATE,'mm') as CREDATE,                          t.ENTRYID,    case when nvl(t1.TOTAL,0)=0 then 0 else nvl(sum(t.TOTAL),0)/nvl(t1.TOTAL,0) end as "同比TOTAL",--同比    case when nvl(t1.SETTLEQTY,0)=0 then 0 else nvl(sum(t.SETTLEQTY),0)/nvl(t1.SETTLEQTY,0) end as "同比SETTLEQTY",--同比        nvl(t1.TOTAL,0) as "同期TOTAL",--同期        nvl(t1.SETTLEQTY,0) as "同期SETTLEQTY",--同期        nvl(t2.TOTAL ,0)as "去年TOTAL",--去年        nvl(t2.SETTLEQTY,0) as "去年SETTLEQTY",--去年        to_char(t.CREDATE,'yyyy-mm') as YEARN,        to_char(t.CREDATE,'yyyy') as YEAR,        nvl(sum(t.TOTAL),0) as TOTAL,--当月        nvl(sum(t.SETTLEQTY),0) as SETTLEQTY--当月from BMS_SA_DTLQRY_V tleft join (select  ENTRYID,        to_char(CREDATE,'yyyymm') as year_mon,        sum(TOTAL) as TOTAL,        sum(SETTLEQTY) as SETTLEQTYfrom BMS_SA_DTLQRY_Vgroup by to_char(CREDATE,'yyyymm'),ENTRYID)t1        on to_char(add_months(t.CREDATE,-12),'yyyymm')=t1.year_mon and t.ENTRYID=t1.ENTRYIDleft join (        select ENTRYID,                to_char(CREDATE,'yyyy') as yearn,                sum(TOTAL) as TOTAL,                sum(SETTLEQTY) as SETTLEQTYfrom BMS_SA_DTLQRY_Vgroup by to_char(CREDATE,'yyyy'),ENTRYID)t2     on  to_char(add_months(t.CREDATE,-12),'yyyy')=t2.yearn and t.ENTRYID=t2.ENTRYIDgroup by to_char(CREDATE,'mm'),        to_char(t.CREDATE,'yyyy-mm'),        to_char(CREDATE,'yyyy'),        t1.TOTAL,                                t.ENTRYID,        t1.SETTLEQTY,        t2.TOTAL,        t2.SETTLEQTY

  • 2关注人数
  • 410浏览人数
  • 最后回答于:2021-6-4 16:41
    请选择关闭问题的原因
    确定 取消
    返回顶部