仅添加上一行和当前行的值 [英] Add the values of previous and current row only

查看:64
本文介绍了仅添加上一行和当前行的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

表名是员工



EmpNo EmpName薪水

1 A 2000

2 B 1000

3 C 3000

4 D 2000

5 E 5000



我需要通过添加前一行和当前行的工资再获得一列



NewSal

2000

3000

4000

5000

7000



我尝试过:



选择EmpNo,

EmpName,薪水,(SELECT SUM(薪水)FROM Employee e1 WHERE

e1.EmpNo< = e2.EmpNo)作为T_Sal

来自员工e2



我试过这个查询但这给了我添加所有先前的行

Table Name is Employee

EmpNo EmpName Salary
1 A 2000
2 B 1000
3 C 3000
4 D 2000
5 E 5000

I need to get one more column by adding salary of previous and current row as

NewSal
2000
3000
4000
5000
7000

What I have tried:

Select EmpNo,
EmpName, Salary,(SELECT SUM(salary) FROM Employee e1 WHERE
e1.EmpNo <= e2.EmpNo)as T_Sal
FROM Employee e2

I tried this query but this is giving me the addition of all previous rows

推荐答案

查看SQL LAG函数: LAG(Transact-SQL)| Microsoft Docs [ ^ ] - 链接包含示例。
Look at the SQL LAG function: LAG (Transact-SQL) | Microsoft Docs[^] - the link includes examples.


这篇关于仅添加上一行和当前行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆