AC Solution


  • 3
    J
    SELECT EA.Id, 
           EA.Month, 
           Sum(EB.salary) AS Salary 
    FROM   (SELECT E1.* 
            FROM   employee E1 
                   LEFT JOIN (SELECT id, 
                                     Max(month) AS month 
                              FROM   employee 
                              GROUP  BY id) E2 
                          ON E1.id = E2.id 
            WHERE  E1.month < E2.month) EA 
           LEFT JOIN employee EB 
                  ON EA.id = EB.id 
    WHERE  EB.month <= EA.month 
           AND EB.month >= EA.month - 2 
    GROUP  BY EA.id, 
              EA.month 
    ORDER  BY EA.id, 
              month DESC 
    

  • 0
    H

    I thought the left join after E1 should be inner join.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.