我正在尝试累积增加上年度期末余额的销售额,并将其转入第一个月,并按月逐月继续。举例来说,如果我们有如下的数据:累计合计从上年度期末余额到当前年度期初余额的销售额
select 4 id, 'A' name, 'group1' group, '2015' year, '10' month, 20 sales from dual union all
select 5 id, 'C' name,'group2' group, '2015' year, '12' month, 89 sales from dual union all
select 13 id,'B' name, 'group2' group, '2016' year, '01' month, 10 sales from dual union all
select 14 id,'A' name, 'group3' group, '2016' year, '02' month, 8 sales from dual union all
select 15 id,'B' name, 'group1' group, '2016' year, '02' month, 16 sales from dual union all
select 16 id,'D' name,'group2' group, '2016' year, '04' month, 15 sales from dual union all
select 17 id,'D' name,'group4' group, '2016' year, '05' month, 23 sales from dual union all
select 18 id,'D' name,'group3' group, '2016' year, '06' month, 39 sales from dual union all
select 19 id,'D' name,'group3' group, '2016' year, '07' month, 45 sales from dual union all
select 20 id,'D' name,'group3' group, '2016' year, '08' month, 12 sales from dual union all
select 21 id,'D' name,'group4' group, '2016' year, '09' month, 20 sales from dual union all
select 22 id,'D' name,'group3' group, '2016' year, '10' month, 4 sales from dual union all
select 23 id,'D' name,'group3' group, '2016' year, '11' month, 98 sales from dual union all
select 24 id,'D' name,'group4' group, '2016' year, '12' month, 70 sales from dual
注意,对于年份= 2015该年度的期末余额为一个月= 12的平衡,在这种情况下是89组2和20组1。如果我们是在2016年,我想累计查询到这样的返回的东西:
year, month, group, opening_balance, closing_balance
2016 01 group2 89 99 (89+10)
2016 02 group3 0 8 (0+8)
2016 02 group1 20 36 (20 + 16)
2016 04 group2 99 114 (99 + 15)
2016 05 group4 0 23 (0 + 23 - note group4 has no prior balances)
2016 06 group3 8 47 (8 + 39)
2016 07 group3 47 92 (47 + 45)
and so on
这看起来像它需要涉及的分析功能和()上(通过分区.... ORDER BY ...)
但我还没有想出正确的方法来做到这一点。
在此先感谢。
你为什么要储存年份和月份为两列文本?最好将它作为一个单独的'DATE'列存储,如果你需要单独的组件,你可以使用'EXTRACT(YEAR FROM sale_date)'或'TO_CHAR(sale_date,'MM')'。 – MT0
您有'group3'开头余额为99 - 但这不会反映在示例数据中。 99从哪里来? – MT0
MT0你是对的。修复。感谢您注意 – bytebiscuit