Created
September 2, 2017 16:31
-
-
Save cixuuz/ad381bd7034b8551c08714031a0c6dcc to your computer and use it in GitHub Desktop.
[579. Find Cumulative Salary of an Employee] #leetcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
E1.id, | |
E1.month, | |
(IFNULL(E1.salary, 0) + IFNULL(E2.salary, 0) + IFNULL(E3.salary, 0)) AS Salary | |
FROM | |
(SELECT | |
id, MAX(month) AS month | |
FROM | |
Employee | |
GROUP BY id | |
HAVING COUNT(*) > 1) AS maxmonth | |
LEFT JOIN | |
Employee E1 ON (maxmonth.id = E1.id | |
AND maxmonth.month > E1.month) | |
LEFT JOIN | |
Employee E2 ON (E2.id = E1.id | |
AND E2.month = E1.month - 1) | |
LEFT JOIN | |
Employee E3 ON (E3.id = E1.id | |
AND E3.month = E1.month - 2) | |
ORDER BY id ASC , month DESC | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment