简体   繁体   English

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

[英]MySQL - SELECT MAX(), and get corresponding fields

The task is: get the list with ID of every employee and the ID of the last department where he worked.任务是:获取每个员工的 ID 和他工作的最后一个部门的 ID 的列表。 It's becoming more complicated cause one person can work in different departments at one time, so we need to get his last department where he has the max rate.这变得越来越复杂,因为一个人可以同时在不同的部门工作,所以我们需要让他的最后一个部门拥有最大的比率。

table:桌子:

 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 is the last day when employee worked, and NULL value means that his contract is actual today. end_date是员工工作的最后一天,NULL 值表示他的合同是今天生效的。

The result must look like:结果必须如下所示:

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

I found out how to select max() with corresponding fields by using join:我发现了如何使用 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

However, there are ALL corresponding rows in the result:但是,结果中有所有相应的行:

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

The question is, how to get NOT JUST corresponding rows to MAX(end_date), but with MAX(rate) too?问题是,如何不仅获得与 MAX(end_date) 对应的行,还获得 MAX(rate) 的对应行? I assume that HAVING might help, but I still don't know what exactly must be there.我认为 HAVING 可能会有所帮助,但我仍然不知道那里必须有什么。

And maybe there are other ways to solve problem with better performance, because this query works about 16s while the table has ~30 000 rows.也许还有其他方法可以更好地解决问题,因为这个查询大约需要 16 秒,而表有大约 30 000 行。

Could you try with the query below:你可以试试下面的查询:

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

Please find the Live Demo请找到Live Demo

UPDATE:更新:

As per the comments the following query helped to achieve the result:根据评论,以下查询有助于实现结果:

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

I think this query will work for you.我认为这个查询对你有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM