MySQL - SELECT MAX(),并获取相应的字段

时间:2016-09-09 12:40:02

标签: mysql

任务是:获取包含每位员工的ID的列表以及他工作的最后一个部门的ID。它变得越来越复杂,因为一个人可以同时在不同的部门工作,所以我们需要让他的最后一个部门获得最高费率。

表:

 ID_employee| ID_department | end_date  |   rate
 1            22              2016-01-01    1
 2            25              NULL          0.3
 2            27              NULL          1
 3            22              2013-12-12    0.5
 3            22              2014-05-05    0.5

end_date是员工工作的最后一天,而NULL值意味着他的合同今天是实际的。

结果必须如下:

ID_employee | ID_department | end_date  |   rate
1             22              2016-01-01    1
2             27              NULL          1
3             22              2014-05-05    0.5

我找到了如何使用join:

选择带相应字段的max()
SELECT table.id_employee, id_department
FROM table
JOIN (  SELECT id_employee,
    IF (MAX( end_date IS NULL ) = 1 , "0000-00-00",  MAX( end_date )) as max_end_date
    FROM table GROUP BY id_employee) maxs ON maxs.id_employee = table.id_employee
WHERE maxs.max_end_date = IFNULL(table.end_date, "0000-00-00")
GROUP BY table.id_employee

但是,结果中有所有相应的行:

ID_employee | ID_department | end_date  |   rate
1             22              2016-01-01    1
2             25              NULL          0.3
2             27              NULL          1
3             22              2014-05-05    0.5

问题是,如何将NOT JUST对应的行输出到MAX(end_date),但是MAX(速率)也是如此?我认为HAVING可能有所帮助,但我仍然不知道究竟应该是什么。

也许还有其他方法可以通过更好的性能来解决问题,因为这个查询的工作时间约为16秒,而表格的行数约为30 000行。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用以下查询:

SELECT  T1.ID_employee,  
        T1.ID_department, 
        CASE WHEN maxs.max_end_date = "0000-00-00" THEN NULL ELSE maxs.max_end_date END AS end_date, 
        T1.rate
FROM TestTable T1
JOIN (  SELECT  id_employee,
                MAX(ID_department) AS ID_department,
                IF (MAX( end_date IS NULL ) = 1, "0000-00-00",  MAX( end_date )) AS max_end_date
        FROM TestTable 
        GROUP BY id_employee ) maxs ON maxs.id_employee = T1.id_employee AND maxs.ID_department = T1.ID_department
WHERE maxs.max_end_date = IFNULL(T1.end_date, "0000-00-00")
GROUP BY T1.id_employee

请找到 Live Demo

<强>更新

根据评论,以下查询有助于实现结果:

SET @CurrentDate := CURDATE();

SELECT T2.ID_employee, 
       T2.ID_department, 
       CASE WHEN MR.Max_end_date = @CurrentDate THEN NULL ELSE T2.end_date END AS end_date, 
       MR.MaxRate AS rate
FROM TestTable T2 
JOIN (
    SELECT T1.ID_employee, MAX(T1.rate) AS MaxRate, MD.Max_end_date
    FROM TestTable T1
    JOIN (
        SELECT  ID_employee,
                MAX(CASE WHEN end_date IS NULL THEN @CurrentDate ELSE end_date END) AS Max_end_date
        FROM TestTable 
        GROUP BY ID_employee
        ) MD ON MD.ID_employee = T1.ID_employee
    WHERE MD.Max_end_date = IFNULL(T1.end_date, @CurrentDate)
    GROUP BY T1.ID_employee
) MR ON MR.ID_employee = T2.ID_employee AND MR.MaxRate = T2.rate 
WHERE MR.Max_end_date = IFNULL(T2.end_date, @CurrentDate)

Working Demo

答案 1 :(得分:0)

我认为此查询适合您。

SELECT ID_employee, ID_department, end_date, MAX(rate)
 FROM test_max
 GROUP BY ID_employee