这个sql怎么实现按dj汇总数据的,dj为1的是所有数据加起来的,等级为2的是它下面的3和4加起来的,等级3是他下面的4加起来的,dj这个字段是自己根据xmdm拆出来的,一共四类,dj怎么拆比较好呢,只能在sql里面写微信截图_20210326162621.png

冰雨咖啡 发布于 2021-3-26 16:30
1min目标场景问卷 立即参与
回答问题
悬赏:3 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共1回答
最佳回答
0
冰雨咖啡Lv5见习互助
发布于2021-3-26 17:05

with ds1 as (

select t1.xmdm,t1.xmmc,loccurramt

from SJYS t1

left join SJYS_DT1 t2 on t1.id=t2.mainid

left join SJYS_KMDM t3 on t2.erpxmdm=t3.glacct2

where t3.fiscalyear=2020 and t3.companycode='W68' and t3.postperiod=1

),

ds2 as (

select t1.xmdm,t1.xmmc,

ds1.loccurramt

from SJYS t1

left join ds1 on t1.xmdm=ds1.xmdm

order by t1.xh

),

ds3 as (

select 'FY00' xmdm,sum(loccurramt) loccurramt from ds2

union all

select substr(xmdm,1,2)||'00' xmdm,sum(loccurramt) loccurramt from ds2

where xmdm <> 'FY00'

group by substr(xmdm,1,2)

union all

select substr(xmdm,1,4) xmdm,sum(loccurramt) loccurramt from ds2

where substr(xmdm,3,2) <> '00'

group by substr(xmdm,1,4)

union all

select substr(xmdm,1,6) xmdm,sum(loccurramt) loccuramt from ds2 where length(xmdm)=6

group by substr(xmdm,1,6)

)

select ds3.xmdm,t1.xmmc,ds3.loccurramt from ds3

join SJYS t1 on t1.xmdm=ds3.xmdm

order by t1.xh

已实现

  • 0关注人数
  • 396浏览人数
  • 最后回答于:2021-3-26 17:05
    请选择关闭问题的原因
    确定 取消
    返回顶部