Anto
Anto

Reputation: 11

How to add a row with another row

How to add a row to another row?

Example:

emp_id  salary  updated salary
1         100      300
2         200      500
3         300      700
4         400

Here in this example, emp_id and salary are two columns. I want to add first and second salary and show it in the first row as 300.

How to do it in SQL?

Upvotes: 1

Views: 113

Answers (2)

koushik veldanda
koushik veldanda

Reputation: 1107

You can go with lead function https://msdn.microsoft.com/en-us/library/hh213125.aspx

SELECT *,Salary + LEAD(Salary, 1,0) OVER (ORDER BY emp_id)"updated salary"
FROM Table

Upvotes: 0

You can use LEAD function.

SELECT emp_id,
       salary,  
       salary + LEAD(salary) OVER (ORDER BY (salary)) AS [updated salary]
FROM Counting

Upvotes: 2

Related Questions