oracle 月销售总金额的累加计算,实现 n = (n-1) + (n-2)

本文介绍如何在Oracle数据库中计算每个月份的累计销售总额,利用sum(amount) over()函数,按照月份顺序逐月累加。通过示例数据和代码,展示了如何获取从第6个月到10月份的销售总金额。

在Oracle 当中如要实现下面这种情况

2 月分的销售总金额= 1 月分金额 + 2 月分的金额

3 月分的销售总金额= 1 月分金额 + 2 月分的金额 + 3 月分的金额。

...

12 月分的销售总金额= 1 + 2 + ... 12 月分金额。

现在在有一张表如数据如下图所示。

现在用 下面代码实现当前月销售总金额累加的计算

select year, month,sum(amount) as month_sum,
sum(sum(amount))over(order by month rows between unbounded preceding and current row)cumulative_amount
from all_sales t group by year,month


主要是 sum(sum(amount))over(order by month rows between unbounded preceding and current row) 句话,按月销售金额进行相加。

第N值= n -1 的值 + n-2 的值。


如果想求第 6 个份 到10月份的话

select year, month,sum(amount) as month_sum,
sum(sum(amount))over(order by month rows between unbounded preceding and current row)cumulative_amount
from all_sales t 
where t.month between 6 and 10
group by year,month



SELECT mm.name AS 药品名称, mm.spec AS 规格, MEASDOC.NAME AS 单位, SUM(quan) AS 数量, SUM(AMT) AS 金额 FROM ( -- 门诊部分 SELECT dt.id_mm, dt.id_pkgu_cur AS id_measdoc, SUM(dt.quan_mp) AS quan, SUM(DT.AMT_REAL) AS AMT FROM MP_DG_OP_DT dt INNER JOIN bl_cg_itm_oep itm ON dt.id_orsrv = itm.id_orsrv INNER JOIN bl_cg_oep oep ON itm.id_cgoep = oep.id_cgoep LEFT JOIN mp_dg_op op ON DT.ID_DGOP = OP.ID_DGOP LEFT JOIN en_ent ent ON ent.id_ent = op.id_ent INNER JOIN bd_mm mm ON dt.id_mm = mm.id_mm WHERE substr(itm.sd_srvtp,0,2) = '01' AND dt.FG_BKFEE = 'N' AND oep.eu_direct = 1 AND ent.Code_Entp = '00' AND itm.fg_refund = 'N' AND itm.fg_st = 'Y' AND mm.fg_keymonitor = 'Y' -- 重点监控药品标志 ${iif(dt_b, "", " and op.dt_dp >= '"+dt_b+" 00:00:00'")} ${iif(dt_e, "", " and op.dt_dp <= '"+dt_e+" 23:59:59'")} ${iif(id_dep, "", " and op.id_dep_mp = '"+id_dep+"'")}--药房 ${iif(id_entp, "", " and op.id_entp = '"+id_entp+"'")}--就诊类型 ${iif(id_mm, "", " and dt.id_mm = '"+id_mm+"'")}--药品名称 ${iif(mzzy, "", " and '1' = '"+mzzy+"' or '0' = '"+mzzy+"' ")} -- 门诊 GROUP BY dt.id_mm, dt.id_pkgu_cur UNION ALL -- 住院部分 SELECT dt.id_mm, dt.id_pkgu_ap AS id_measdoc, SUM(dt.quan_ap) AS quan, SUM(DT.AMT) AS AMT FROM mp_dg_ip_apde_dt dt INNER JOIN mp_dg_ip_de de ON dt.id_dgipde = de.id_dgipde INNER JOIN ci_order der ON der.id_or = dt.id_or INNER JOIN en_ent ent ON ent.id_ent = dt.id_ent INNER JOIN bd_mm mm ON dt.id_mm = mm.id_mm WHERE NVL(dt.id_dgipde,'~') <> '~' AND dt.en_opde = 1 AND mm.fg_keymonitor = 'Y' -- 重点监控药品标志 ${iif(dt_b, "", " and de.dt_dp >= '"+dt_b+"'")} ${iif(dt_e, "", " and de.dt_dp <= '"+dt_e+"'")} ${iif(id_dep, "", " and dt.id_dep_wh = '"+id_dep+"'")} ${iif(id_entp, "", " and dt.id_entp = '"+id_entp+"'")} ${iif(id_mm, "", " and dt.id_mm = '"+id_mm+"'")} ${iif(mzzy, "", " and '2' = '"+mzzy+"' or '0' = '"+mzzy+"' ")} -- 住院 GROUP BY dt.id_mm, dt.id_pkgu_ap UNION ALL -- 手术部分 SELECT dt.id_mm, dt.id_pkgu_ap AS id_measdoc, SUM(dt.quan_ap) AS quan, SUM(dt.price * dt.quan_ap) AS amt FROM mp_dg_ip_apde_dt dt LEFT JOIN mp_dg_ip_ap ap ON dt.id_dgipap = ap.id_dgipap LEFT JOIN bd_dep depor ON dt.id_dep_or = depor.id_dep INNER JOIN bd_mm mm ON dt.id_mm = mm.id_mm WHERE NVL(dt.id_dgipde, '~') <> '~' AND dt.en_opde = 1 AND dt.quan_ap > 0 AND dt.fg_invalid = 'N' AND depor.def1 = '医技科室' AND mm.fg_keymonitor = 'Y' -- 重点监控药品标志 ${iif(dt_b, "", " and ap.modifiedtime >= '"+dt_b+"'")} ${iif(dt_e, "", " and ap.modifiedtime <= '"+dt_e+"'")} ${iif(id_dep, "", " and dt.id_dep_wh = '"+id_dep+"'")} ${iif(id_entp, "", " and dt.id_entp = '"+id_entp+"'")} ${iif(id_mm, "", " and dt.id_mm = '"+id_mm+"'")} ${iif(mzzy, "", " and '2' = '"+mzzy+"' or '0' = '"+mzzy+"' ")} -- 住院 GROUP BY dt.id_mm, dt.id_pkgu_ap ) drug_data INNER JOIN bd_mm mm ON drug_data.id_mm = mm.id_mm LEFT JOIN bd_measdoc measdoc ON drug_data.id_measdoc = measdoc.id_measdoc WHERE mm.fg_keymonitor = 'Y' -- 外层再次确认重点监控药品 GROUP BY mm.name, mm.spec, MEASDOC.NAME ORDER BY SUM(quan) DESC;给你根据以上代码,帮我把金额换成本金额,和上金额两个字段,用orcale数据库
最新发布
11-22
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值