查询每个工作中心的最新状态

时间:2016-12-06 23:11:38

标签: sql ms-access-2010

我有一张桌子:

Workcenter | Date | Status ------------------------------ SLX83A 12/6/2016 3:11:20 PM Running SLX83B 12/6/2016 3:28:33 PM Break/Lunch/Meeting SLX83B 12/2/2016 10:11:37 AM Breakdown SLX83B 12/6/2016 3:28:29 PM Running SLX83B 12/6/2016 1:30:21 PM Tool Change/Damage SLX83B 12/1/2016 3:13:43 PM Waiting On Crane SLX83C 12/1/2016 8:10:56 AM Break/Lunch/Meeting SLX83C 11/27/2016 6:40:00 AM Load/Unload Clamp Change

我想输出每个工作中心的最新状态,例如:

Workcenter | Date | Status ------------------------------ SLX83A 12/6/2016 3:11:20 PM Running SLX83B 12/6/2016 3:28:33 PM Break/Lunch/Meeting SLX83C 12/1/2016 8:10:56 AM Break/Lunch/Meeting

我可以通过以下方式获得最近的时间:

SELECT [Current Status Query].[Workcenter ID], Max([Current Status Query].[MaxOfStart Date/Time]) AS [MaxOfMaxOfStart Date/Time] FROM [Current Status Query] GROUP BY [Current Status Query].[Workcenter ID];

=

Workcenter | Date ------------------------------ SLX83A 12/6/2016 3:11:20 PM SLX83B 12/6/2016 3:28:33 PM SLX83C 12/1/2016 8:10:56 AM

但是我无法弄清楚如何将状态''标记为“。”

1 个答案:

答案 0 :(得分:0)

SELECT t1.*
FROM
    Table t1
    LEFT JOIN Table t2
    ON t1.workcenter = t2.workcenter
    AND t1.Date < t2.Date
WHERE
    t2.workcenter IS NULL

你可以做ANTI SELF JOIN。基本上left join自己t1.Date小于t2.Date的表格,以查找没有后续行的行,然后过滤掉匹配的行。