oracle 每个月求本年该月及之前的合计
问题具体描述:201901值为201901值,201902值为201901+201902,201903值为201901+201902+201903,以此类推
模拟表格:table 字段为 month value
模拟数据:
month | value |
---|---|
201811 | 120 |
201812 | 321 |
201901 | 112 |
201902 | 165 |
201903 | 75 |
sql
select t2.month,sum(t1.value) value from table t1 left join
(select distinct month from table) t2
on substr(t1.month,1,4) = substr(t2.month,1,4)
– on t1.month between substr(t2.month,1,4)||‘01’ and t2.month
–两个连接条件都可以
and t1.month <= t2.month group by t2.month