SQL小提琴在这里http://sqlfiddle.com/#!9/dd34e6/1
有多个带有不同时间戳的任务条目。我正在尝试获取每个taskId的最新条目,如预期结果所示。
架构:
CREATE TABLE TaskStatuses
(
taskId INT,
status VARCHAR(255),
timestamp DATETIME
);
当前结果:
1 Complete 1530295000
3 Complete 1530294900
3 Active 1530294100
1 Active 1530294000
4 Complete 1530293900
2 Active 1530293500
1 Paused 1530293000
1 Active 1530292000
4 Active 1530292000
所需结果:
1 Complete 1530295000
3 Complete 1530294900
4 Complete 1530293900
2 Active 1530293500
的解决方案
SELECT a.taskId, a.status, unix_timestamp(a.timestamp) as unix
FROM TaskStatuses a
INNER JOIN (
SELECT taskId, MAX(timestamp) timestamp
FROM TaskStatuses
GROUP BY taskId
) b ON a.taskId = b.taskId AND a.timestamp = b.timestamp