Showing posts with label 165 ) 5) SSMS - Cumulative Salary of Each Employee by Department. Show all posts
Showing posts with label 165 ) 5) SSMS - Cumulative Salary of Each Employee by Department. Show all posts

Monday, August 10, 2026

165 ) 5) SSMS - Cumulative Salary of Each Employee by Department

 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5) SSMS - Cumulative Salary of Each Employee by Department

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Question: Write a query to calculate the cumulative (running total) salary of each employee ordered by their salary within their respective department.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Query:



SELECT 

    DepartmentID,

    EmployeeID,

    EmployeeName,

    Salary,

    SUM(Salary) 


            OVER     (


                PARTITION BY DepartmentID 


                    ORDER BY Salary 


                            ROWS BETWEEN


                                 UNBOUNDED PRECEDING AND CURRENT ROW)


             AS    CumulativeSalary


FROM Employees;

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

Sample Output (2 Rows):

DepartmentIDEmployeeIDEmployeeNameSalaryCumulativeSalary
10101Alice50005000
10102Bob700012000

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    173 ) 14 ) Query to find duplicate rows of loan id but time is different

      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 14 )  Query to find duplicate rows of loan id but time is different  ~~~~~~~~~...