Oracle中的累计计算
Cumulative calculation in Oracle
我下面的 Emp
table 包含三列 (empno, ename, salary
)。
empno ename salary
---------------------
101 Ram 2000
102 Kumar 1000
103 Mani 3000
我需要通过在 table.
上方显示 Cumulative_Salary
来输出以下内容
Empno Ename Cumulative_Salary
-------------------------------
101 Ram 2000
102 Kumar 3000
103 Mani 6000
您正在寻找 SUM OVER
。
select
empno, ename, salary,
sum(salary) over(order by empno) as cumulative_salary
from emp
order by empno;
通过在 OVER
子句中应用 ORDER BY empno
,我们隐含地应用了 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
,但如果您愿意,您当然可以将其明确化。
文档:https://oracle-base.com/articles/misc/sum-analytic-function
select
empno
, ename
, sum(salary) as cumulative_salary
from emp
group by
empno, ename
order by empno;
我下面的 Emp
table 包含三列 (empno, ename, salary
)。
empno ename salary
---------------------
101 Ram 2000
102 Kumar 1000
103 Mani 3000
我需要通过在 table.
上方显示Cumulative_Salary
来输出以下内容
Empno Ename Cumulative_Salary
-------------------------------
101 Ram 2000
102 Kumar 3000
103 Mani 6000
您正在寻找 SUM OVER
。
select
empno, ename, salary,
sum(salary) over(order by empno) as cumulative_salary
from emp
order by empno;
通过在 OVER
子句中应用 ORDER BY empno
,我们隐含地应用了 RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
,但如果您愿意,您当然可以将其明确化。
文档:https://oracle-base.com/articles/misc/sum-analytic-function
select
empno
, ename
, sum(salary) as cumulative_salary
from emp
group by
empno, ename
order by empno;