简体   繁体   English

Ms Access/SQL 查询

[英]Ms Access/ SQL query

I have a table as below.我有一张如下表。 Need a Query to select the Last activity of the User.需要一个查询来选择用户的最后一个活动。 (There should not be multiple listing for same user, Single user single row) (同一用户不应有多个列表,单用户单行)

单击此处查看表

+----------------+---------+--------------------+-----------------+------------+
|      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 |
+----------------+---------+--------------------+-----------------+------------+

You can try this query.你可以试试这个查询。 The subselect calculate max(lastmodified) for each user, using GROUP BY.子选择使用 GROUP BY 为每个用户计算 max(lastmodified)。 The result is joined to main table using BOT_ID and the calculated date with lastmodified date.结果使用 BOT_ID 和计算日期与上次修改日期连接到主表。 If a user has two same date lastmodified, both the rows will be returned:如果用户有两个相同的上次修改日期,则将返回这两行:

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

You can use:您可以使用:

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

To list all:要列出所有:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM