SQL 新手。在 Access 2016 中。在不同设备(EQUIP1、EQUIP2、EQUIP3)的表中,我想查询最后一个和倒数第二个维护日期。
我搜索的许多修复不考虑按 ID 分组(在我的情况下为 EQUIP#)
是)我有的:
TABLE Maintenance
equipment Date
1 1/1/2019
1 1/2/2019
1 1/3/2019
2 2/1/2019
2 2/2/2019
2 2/3/2019
我需要的:
QUERY LATESTDATES
equipment NewDate PreviousDate
1 1/3/2019 1/2/2019
2 2/3/2019 2/2/2019
编辑:谢谢!有点赶上语法,但这是我的最终解决方案:
SELECT [a1].equipment, NewDate, Max([b].Date) as PreviousDate
FROM
(SELECT equipment,Max(Date) as NewDate
FROM Maintenance AS [A]
GROUP BY equipment) AS [a1]
INNER JOIN Maintenance AS [b]
ON [b].equipment= [a1].equipment AND [b].Date <> [a1].NewDate
GROUP BY [a1].equipment, [a1].NewDate