[英]Select latest entry of two distinct columns
I've been at it for hours trying to figure this out but no luck. 我已经花了几个小时试图解决这个问题,但没有运气。 I'm trying to get the most recent change to the machine status.
我正在尝试获取机器状态的最新更改。 Machines have unique names and belong to a few different classes.
机器具有唯一的名称,属于几个不同的类。 The following query is the closest I've gotten to what I want, but it doesn't return the latest result.
以下查询是我最接近我想要的,但它不会返回最新结果。 Just distinct Class and Name values.
只是不同的类和名称值。
SELECT * FROM ( SELECT * FROM MachineStatus ORDER BY date DESC ) AS ord GROUP BY class, name
The data table looks like this, but with the rows mixed up. 数据表看起来像这样,但行混合了。
Class Name Status Date A NXP ONLINE 2015-02-17 A NXP OFFLINE 2015-02-10 A AVR STANDBY 2015-02-16 A AVR OFFLINE 2015-01-05 A PIC ONLINE 2015-02-01 A PIC STANDBY 2015-01-05 B TXI ONLINE 2015-02-10 B TXI STANDBY 2015-02-07 B FSC ONLINE 2015-02-17 B FSC OFFLINE 2015-02-01 C STM OFFLINE 2015-02-17 C STM STANDBY 2015-02-10 C INT ONLINE 2015-01-01 C INT STANDBY 2014-11-10
What I'm trying to get is: 我想要得到的是:
A NXP ONLINE 2015-02-17 A AVR STANDBY 2015-02-16 A PIC ONLINE 2015-02-01 B TXI ONLINE 2015-02-10 B FSC ONLINE 2015-02-17 C STM OFFLINE 2015-02-17 C INT ONLINE 2015-01-01
Many thanks! 非常感谢!
SELECT t.Class, t.Name, t.Status, t.Date
FROM my_table t
INNER JOIN (SELECT Class, Name, MAX(Date) AS Date
FROM my_table
GROUP BY Class, Name
) sub ON (sub.Class = t.Class AND sub.Name = t.Name AND sub.Date = t.Date)
GROUP BY t.Class, t.Name
应该像......一样直截了当
SELECT `table`.* FROM `table` GROUP BY `name` HAVING `date` = MAX(`date`)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.