首页 > 代码库 > mysql 累计值计算
mysql 累计值计算
由于mysql没有很好的支持分析函数,现在以SUM分析函数为例,做一下mysql的按日累计值计算
mysql 累计计算 select DateID TRAN_DT, REGI_CH REGI_CH, TRAN_CH TRAN_CH, IFNULL(DRAW_CT, 0) DRAW_CT, round(IFNULL(DRAW_AMT, 0),2) DRAW_AMT, ALL_DRAW_CT, round(ALL_DRAW_AMT, 2) ALL_DRAW_AMT from (select d.DateID, d.REGI_CH REGI_CH, d.TRAN_CH TRAN_CH, DRAW_CT, DRAW_AMT, @rownum := @rownum + 1, IF(@REGI_CH = d.REGI_CH and @TRAN_CH = d.TRAN_CH, @ALL_DRAW_CT := @ALL_DRAW_CT + ifnull(b.DRAW_CT, 0), @ALL_DRAW_CT := b.DRAW_CT) AS ALL_DRAW_CT, IF(@REGI_CH = d.REGI_CH and @TRAN_CH = d.TRAN_CH, @ALL_DRAW_AMT := @ALL_DRAW_AMT + ifnull(b.DRAW_AMT, 0), @ALL_DRAW_AMT := b.DRAW_AMT) AS ALL_DRAW_AMT, IF(@REGI_CH = d.REGI_CH and @TRAN_CH = d.TRAN_CH, @rank := @rank + 1, @rank := 1) AS rank, @REGI_CH := d.REGI_CH, @TRAN_CH := d.TRAN_CH FROM (select dateid, REGI_CH, TRAN_CH from (select d.dateid, REGI_CH, TRAN_CH, TRAN_DT from (select TRAN_DT, REGI_CH, TRAN_CH, IF(@REGI_CH = b.REGI_CH and @TRAN_CH = b.TRAN_CH, @rank := @rank + 1, @rank := 1) AS rank, @REGI_CH := b.REGI_CH, @TRAN_CH := b.TRAN_CH FROM (select TRANDT TRAN_DT, u.REGI_CH, TRANCH TRAN_CH from ods_bd_withdraw t join ods_bd_user_person u on u.member_cd = t.member_cd where trandt > ‘20150515‘ and trandt < ‘${trandate}‘ group by trandt, u.REGI_CH, TRANCH order by u.REGI_CH, TRANCH, trandt) b, (SELECT @REGI_CH := NULL, @TRAN_CH := null, @rank := 0) c having rank = 1) n join dim_date d on 1 = 1 where d.dateid > ‘20150515‘ and d.dateid < ‘${trandate}‘ ) f where f.TRAN_DT <= dateid order by REGI_CH, TRAN_CH, dateid) d left join (select TRANDT TRAN_DT, u.REGI_CH, TRANCH TRAN_CH, count(*) DRAW_CT, sum(TRANAM) DRAW_AMT from ods_bd_withdraw t join ods_bd_user_person u on u.member_cd = t.member_cd where trandt > ‘20150515‘ and trandt < ‘${trandate}‘ group by trandt, u.REGI_CH, TRANCH order by u.REGI_CH, TRANCH, trandt) b on b.TRAN_DT = d.DateID and d.REGI_CH = b.REGI_CH and d.TRAN_CH = b.TRAN_CH join (SELECT @rownum := 0, @REGI_CH := NULL, @TRAN_CH := null, @ALL_DRAW_CT := 0, @ALL_DRAW_AMT := 0, @rank := 0) c on 1 = 1 order by dateid) t
mysql 累计值计算
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。