create table saltst ( empid int, saldate datetime, salary int ) --Insert Records For Each Employee Insert into saltst values(1, '2014-01-01', 1000) Insert into saltst values(1, '2014-03-26', 3400) Insert into saltst values(2, '2014-01-25', 2000) Insert into saltst values(2, '2014-06-11', 3000) Insert into saltst values(2, '2014-09-01', 4000) Insert into saltst values(3, '2013-01-01', 4000) Insert into saltst values(3, '2014-01-01', 1000) Insert into saltst values(4, '2014-11-01', 3500) -- Run Select select * from saltst -- Add DENSE_RANK() Select empid, saldate, salary, DENSE_RANK() OVER(partition by empid order by saldate) as dr From saltst Go -- Solution - Substract Last Salary From Previous Salary with SalaryRank as ( select empid, saldate, salary, DENSE_RANK() OVER(partition by empid order by saldate) as dr from saltst), Currentsal as (select empid, salary, saldate from SalaryRank a where dr in(select max(dr) from SalaryRank b where a.empid=b.empid)), PreviousSal as (select empid, salary, saldate from SalaryRank a where dr in(select case when max(dr) = 1 then 1 else max(dr) -1 end mx from SalaryRank b where a.empid=b.empid)) --select * from SalaryRank; --select * from Currentsal; --select * From PreviousSal; select a.empid, a.salary as Currentsal, b.salary as PreviousSal, a.salary - b.salary as SalDiff, a.saldate As CurrentSalDate, b.saldate As PreviousSalDate from (select * from Currentsal) a join (select * from PreviousSal) b on a.empid = b.empid