2017-09-26 182 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() –