SQL Help: Calculating salary change for employees

timswim78

Diamond Member
Jan 1, 2003
4,330
1
81
Hi! I am trying to calculate the difference in salaries, from one year to the next for every employee.

For the sake of simplicity, I have a table that takes a snapshot of employee salaries once a year. It has these fields:
ID, SALARY, YEAR

I want to use SQL to create a new table that contains each employees ID and the difference in their salary between the 2011 snapshot and the 2012 snapshot. It simply needs to contain:
ID, SALCHANGE

SALCHANGE will be the difference between the employees salary at the 2011 snapshot and the 2012 snapshot.

My current solution is very crude. I selected all of the id's and salaries for 2011 into one table and all of the ids and salaries for 2012 into another table. I then joined the 2011 and 2012 tables and created a new column that is the difference between the 2011 and 2012 salary values.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
You don't need two tables for that.

Select A.ID, A.Salary as Sal2011, B.Salary as Sal2012, B.Salary-A.Salary as SalChange
from Employees A, Employees B
where A.Year = 2011 and B.Year = 2012 and A.ID = B.ID
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
^ Good response. I see too many people write SQL like they are trying to work an Excel document.

Take PhatoseAlpha's response and add an "Select into" your table name set up with those columns and you're good to go.

Edit: Bonehead
 
Last edited:

timswim78

Diamond Member
Jan 1, 2003
4,330
1
81
You don't need two tables for that.

Select A.ID, A.Salary as Sal2011, B.Salary as Sal2012, B.Salary-A.Salary as SalChange
from Employees A, Employees B
where A.Year = 2011 and B.Year = 2012 and A.ID = B.ID

Perfect! Thanks. I'll slap this into a loop and run it for all the years that I need.
 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
Code:
SELECT Start.EmployeeID, 
       Start.Year, 
       Start.Salary as SalaryBeginning, 
       End.Year, 
       End.Salary as SalaryEnding, 
       End.Salary-Start.Salary as SalaryChange
FROM Employees Start
LEFT JOIN Employees End on Start.EmployeeID = End.EmployeeID and (Start.Year + 1)= End.Year
WHERE Start.Year BETWEEN 2006 AND 2011

Loops should be avoided if at all possible.
 
Last edited:

rifken2

Member
Feb 1, 2010
140
0
71
Agree with PhatoseAlpha and JustAnAverageGuy... Self join the table and use the between clause... No need for a loop and no need for a second table...
 

ForumMaster

Diamond Member
Feb 24, 2005
7,797
1
0
What you want is quite standard. you want to use what is known as the "lead analytical function". exists in oracle since version 9i (circa 2001) and finally sql server is copying it in version 2012 (it took 11 years!).

basic syntax goes like this:

LEAD (value_expression [,offset] [,default]) OVER ([query_partition_clause] order_by_clause)

so to explain better and give an actual working example:

SELECT e.EmployeeID,
e.Year,
e.Salary - lead(e.salary, 1) over(partition by e.year order by e.employeedid) as salary_difference
FROM Employee e;

why is this better? one select, no joins, one sort and therefor much faster.

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |