oracle 我应该写什么查询从工资表中计算员工的累计工资

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19035738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:57:20  来源:igfitidea点击:

What query should I write to calculate the cumulative salary of employees from the salary table

sqloracleoracle9i

提问by

A salary table is given and we need to write a single query which prints the cumulative salary of employees. For Example:

给出了一个工资表,我们需要编写一个查询来打印员工的累计工资。例如:

Emp  Sal
A    10
B    15
C    20
D    5


OutPut:

Emp  Sal   Cum_Sal
A    10      10
B    15      25
C    20      45
D    5       50

采纳答案by Roman Pekar

in some RDBMS (like PostgreSQL or SQL Server 2012 or Oracle) it's possible to use window function:

在某些 RDBMS(如 PostgreSQL 或 SQL Server 2012 或 Oracle)中,可以使用窗口函数:

select
    Emp, Sal, sum(Sal) over(order by Emp) as Cum_Sal
from employees

if it's not possible, you can use self-join

如果不可能,您可以使用自连接

select
    e1.Emp, e1.Sal, sum(e2.Sal) as Cum_Sal
from employees e1
    inner join employees e2 on e2.Emp <= e1.Emp
group by e1.Emp, e1.Sal

sql fiddle demo

sql fiddle demo

Note that in both cases you have to define order for rolling sum (in queries above, it's ordered by Empcolumn).

请注意,在这两种情况下,您都必须定义滚动总和的顺序(在上面的查询中,它是按Emp列排序的)。

回答by suresh kumar

For MS SQL please try this:

对于 MS SQL,请尝试以下操作:

select NAME,SAL,sum(SAL) 
over(order by SAL rows between unbounded preceding and current row) as cum_sal 
from employee

回答by user6247426

enter image description here

在此处输入图片说明

select e.name,e.joining,sum(e_1.increment) from employee e inner join employee e_1 on e.name=e_1.name where e.joining>=e_1.joining group by e.name,e.joining

table is like below you can use same approach

表格如下所示,您可以使用相同的方法