最近做數據分析,需要用到累加功能,發現強大的oracle還真有,用over(order by field)
例子:
數據表中最后一列就是累加的效果
累加sql:
select t.acc_pedal_pos,count(*) num,sum(count(*)) over(order by t.acc_pedal_pos) accu_sum from GT1117CARDATA t where t.acc_pedal_pos>0 group by t.acc_pedal_pos order by t.acc_pedal_pos
根據累計求和,進一步求占總和的百分比
sql:
--計算累計百分比,先求列和,然后嵌套求百分比
select t1.*,round(t1.accu_sum/t2.allsum*100,2)||'%' from (select t.acc_pedal_pos,
count(*) num,
sum(count(*)) over(order by t.acc_pedal_pos) accu_sum
from GT1117CARDATA t
where t.acc_pedal_pos > 0
group by t.acc_pedal_pos
order by t.acc_pedal_pos)t1,(select count(acc_pedal_pos) allsum from GT1117CARDATA where acc_pedal_pos>0) t2