2017-09-26 207 views
-3

我有一个表如下。需要查询来选择用户的最后一项活动。 (这里不应该是相同的用户,单用户单排多个上市)女士访问/ SQL查询

Click here for Table

+----------------+---------+--------------------+-----------------+------------+ 
|  Date  | BOT_ID |  Process  | LastModified | Status | 
+----------------+---------+--------------------+-----------------+------------+ 
| 17/09/26 00:00 | User 1 | Interaction record | 9/19/2017 10:50 | In Process | 
| 17/09/26 00:00 | User 2 | Single Assessed | 9/26/2017 12:00 | In process | 
| 17/09/26 00:00 | User 3 | Interaction record | 9/26/2017 11:35 | Completed | 
| 17/09/26 00:00 | User 4 | Metering   | 9/26/2017 11:50 | In proce | 
| 17/09/26 00:00 | User 1 | Move In   | 9/26/2017 11:50 | In process | 
| 17/09/26 00:00 | User 4 | Interaction record | 9/26/2017 11:58 | Completed | 
| 17/09/26 00:00 | User 5 | Direct Debit  | 9/26/2017 11:10 | Completed | 
| 17/09/26 00:00 | User 17 | latest    | 9/26/2017 0:15 | In Process | 
+----------------+---------+--------------------+-----------------+------------+ 
+1

问题是? –

+0

您对Access或MySQL有疑问吗?请只使用适当的标签。 – Barmar

+0

在MySQL中它会是:https://stackoverflow.com/questions/7745609/sql-select-only-rows-with-max-value-on-a-column?rq=1也许它在Access中类似。 – Barmar

回答

0

您可以使用:

Select Max([Last Modified]) As [Last Activity] 
From YourTable 
Where BOT_ID = "User 1" 

要列出所有:

Select BOT_ID, Max([Last Modified]) As [Last Activity] 
From YourTable 
Group By BOT_ID 
+0

但是,我需要列出每个用户的最后一项活动。 –

+0

好吧,看到扩展的答案。 – Gustav

+0

是的!这也是有效的。感谢您的宝贵努力.. –

1

你可以试试这个查询。子选择使用GROUP BY为每个用户计算max(lastmodified)。结果使用BOT_ID和上次修改日期的计算日期连接到主表。如果用户最后修改了两个相同的日期,则两行都将被返回:

SELECT A.* 
FROM YOURTABLE A 
INNER JOIN (SELECT BOT_ID, MAX(LASTMODIFIED) AS LAST_ACTIVITY 
      FROM YOURTABLE 
      GROUP BY BOT_ID) B ON A.BOT_ID=B.BOT_ID AND A.LASTMODIFIED = B.LAST_ACTIVITY 
+0

Ye ..工作。感谢esta。 小修改,用Last()代替max() –