这几天一直在搞报表,发现Oracle真的很强大,需要用到累加功能,发现强大的oracle还真有,用over(order by field)解决的
例子:
数据表中最后一列就是累加的效果
累加sql:
select substr(hn.fd_sjsxsj_time,1,7) 日期,
sum(hn.fd_sh_workload) 工作量,
sum(sum(hn.fd_sh_workload)) over(order by substr(hn.fd_sjsxsj_time,7)) 累加
from tjstat.his_need hn
where substr(hn.fd_sjsxsj_time,4) = '2017'
group by substr(hn.fd_sjsxsj_time,7)
order by substr(hn.fd_sjsxsj_time,7);
趁热打铁,根据累计求和,进一步求占总和的百分比
例子:
sql:
思路:计算累计百分比,先求列和,然后嵌套求百分比
select t1.*,round(t1.accu_sum/t2.allsum*100,2)||'%' from (select substr(hn.fd_sjsxsj_time,
sum(hn.fd_sh_workload) 工作量,
sum(sum(hn.fd_sh_workload)) over(order by substr(hn.fd_sjsxsj_time,7)) accu_sum
from tjstat.his_need hn
where substr(hn.fd_sjsxsj_time,7))t1,(select sum(hn.fd_sh_workload) allsum from tjstat.his_need hn where substr(hn.fd_sjsxsj_time,4) = '2017') t2
原文链接:https://www.f2er.com/oracle/208274.html