bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 579. Find Cumulative Salary of an Employee

579. Find Cumulative Salary of an Employee

Leetcode Solutions

Cumulative Salary Summary Using Self-Joins and Conditional Aggregation

  1. Join the Employee table with itself (as E2) on id and month - 1 to get the salary of the previous month.
  2. Join the Employee table with itself again (as E3) on id and month - 2 to get the salary of the month before the previous month.
  3. Use IFNULL to handle cases where there is no salary for the previous months by treating them as 0.
  4. Calculate the 3-month cumulative salary by summing the salaries from the current month, the previous month, and the month before the previous month.
  5. Create a subquery to find the most recent month for each employee where they have more than one record.
  6. Use a LEFT JOIN to join the subquery with the cumulative salary result and filter out the most recent month's salary for each employee.
  7. Order the final result by id in ascending order and by month in descending order.
erDiagram
    Employee {
        int id
        int month
        int salary
    }

Cumulative Salary Summary Using Window Functions

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...