Skip to content

579. Find Cumulative Salary of an Employee 👎

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
WITH
  CurrMonth AS (
    SELECT
      *,
      MAX(month) OVER(PARTITION BY id) AS max_month
    FROM Employee
  )
SELECT
  CurrMonth.id,
  CurrMonth.month,
  SUM(PrevMonth.salary) AS salary
FROM CurrMonth
INNER JOIN Employee AS PrevMonth
  ON (CurrMonth.id = PrevMonth.id AND CurrMonth.month - PrevMonth.month BETWEEN 0 AND 2)
WHERE CurrMonth.month != CurrMonth.max_month
GROUP BY 1, 2
ORDER BY 1, 2 DESC;