缓慢变化事实表抽取月粒度聚集表

select * from (
select YM,
        SJ_QYLX,
       SJ_HYDM HYDM,
       HYDM HYDM_GB,
       JJHKLB,
       QYLX,
       DJJG,
       GXGSS,
       QYNL,
       ZJGM,
       count(case when jyzt = '01' then nbxh else null end) HS_CL,
       count(case when trunc(clrq,'MM') = trunc(YM_LDAY,'MM')  then nbxh else null end) HS_ZL,
       count(case when jyzt = '02' and trunc(YM_LDAY,'MM') = trunc(hzrqq,'MM') then nbxh else null end) HS_ZX,
       count(case when (jyzt = '03' or jyzt='06' or jyzt='12') and trunc(YM_LDAY,'MM') = trunc(hzrqq,'MM') then nbxh else null end) HS_DX,
       count(case when jyzt = '04' and trunc(YM_LDAY,'MM') = trunc(hzrqq,'MM')  then nbxh else null end) HS_QC,
       sum(case when jyzt = '01' then (YM_LDAY-clrq)/12 else 0 end) NL,
       sum(case when jyzt = '01' then zczb else 0 end) ZCZB,
       sum(case when trunc(clrq,'MM') = trunc(YM_LDAY,'MM') then zczb else 0 end) ZCZB_ZL
from (select t1.ym,
                t2.SJ_QYLX,
               t2.gxgss,
               t2.djjg,
               t2.jyzt,
               t2.qylx,
               t2.sj_hydm,
                 t2.hydm,
               t2.jjhklb,
               t2.zczb,
               t2.NBXH,
               YM_LDAY,
               t2.clrq,
               t2.hzrqq,
        CASE 
 when  (t1.YM_LDAY-t2.clrq)/365>=0 and (t1.YM_LDAY-t2.clrq)/365<6 then  1
 when  (t1.YM_LDAY-t2.clrq)/365>=6  and (t1.YM_LDAY-t2.clrq)/365<10 then 2
 when (t1.YM_LDAY-t2.clrq)/365>=10  and (t1.YM_LDAY-t2.clrq)/365<20 then 3
 else  4   end   qynl,
CASE
WHEN t2.zczb>=0 and t2.zczb<100 then 1
WHEN t2.zczb>=100 and t2.zczb<1000 then 2
WHEN t2.zczb>=1000 and t2.zczb<3000 then 3
WHEN t2.zczb>=3000 then 4
ELSE 5 END AS ZJGM
          from (select * from DM_YMONTH where trunc(YM_LDAY,'YY')<=trunc(sysdate,'YY')) t1
          full join  F_JJHK t2
            on t2.hzrqq <= t1.YM_LDAY
           and t2.hzrqz >= t1.YM_LDAY
        and t2.clrq<=t1.YM_LDAY where t1.ym is not null) tt
group by tt.ym, SJ_QYLX,djjg, gxgss,sj_hydm,hydm, jjhklb,qynl,zjgm,qylx
) t 
where not (hs_cl =0 and hs_zl =0 and hs_zx=0 and hs_dx=0 and hs_qc=0 and zczb=0 and nl=0 and ZCZB_ZL=0)

猜你喜欢

转载自www.cnblogs.com/sorliran/p/9181978.html