Tuesday, 16 January 2018

Write Sql query to calculate total salary distribution row by row

create table #temp
(
  Id int identity,
  Name nvarchar(100),
  SalaryDeduction decimal(18,2),
  Salary decimal(18,2)
)


insert into #temp(Name,SalaryDeduction,Salary) values('A',1000,10000),('B',2000,15000),('C',500,10000),('D',200,20000),('E',150,12000),('F',1400,13000)

Output like below-









Id    Name    SalaryDeduction    Salary    totalSalary
1    A            1000.00     10000.00           10000.00
2    B            2000.00    15000.00           25000.00
3    C            500.00      10000.00          35000.00
4    D           200.00       20000.00          55000.00
5    E           150.00       12000.00          67000.00
6    F           1400.00     13000.00         80000.00

Sql query-

 select Id,
       Name,
       SalaryDeduction,
       Salary,
       (
         select Sum(t.Salary)
         from #temp t where t.Id <= t1.Id
        ) as totalSalary
       from #temp t1


No comments:

Post a Comment