桌子:
| ID | 行動 | 專案編號 | 創建于 | last_updated_on |
|---|---|---|---|---|
| 1 | 更新 | 123 | 2021.1.1 | 2021.5.3 |
| 2 | 創建 | 123 | 2021.1.4 | 2021.5.2 |
| 3 | 更新 | 123 | 2021.1.3 | 2021.5.1 |
| 4 | 更新 | 456 | 2021.2.1 | 2021.6.3 |
| 5 | 更新 | 456 | 2021.2.2 | 2021.6.2 |
| 6 | 創建 | 456 | 2021.2.3 | 2021.6.1 |
我想獲得一張地圖project及其最近創建Update的動作last_updated_on。
我的說法是
select project_id, last_updated_on
from table
where action = "Update"
group by project_id
order by created_on desc
limit 1
但我得到了SELECT list is not in GROUP BY clause and contains nonaggregated column.
我無法更改ONLY_FULL_GROUP_BY屬性。
在我的用例中,我認為我不能添加last_updated_on任何group by一個。
我也試過
select
project_id,
(select last_updated_on where created_on = max(created_on)) as "last_updated_on"
.
.
.
但錯誤仍然發生。
希望可以有人幫幫我。Tnanks 提前!
更新:
示例表的期望結果:
| 專案編號 | last_updated_on |
|---|---|
| 123 | 2021.5.1 |
| 456 | 2021.6.2 |
解釋:
- 對于
project_id = 123,第 1 行和第 3 行action是update, - 之后
order by created_on desc limit 1,第 3 行被選中,其last_updated_on為2021.5.1。
uj5u.com熱心網友回復:
你可以做:
select project_id, last_updated_on
from (
select *, row_number() over(partition by project_id
order by last_updated_on desc) as rn
from t
where action = 'Update'
) x
where rn = 1
order by created_on
將顯示每個專案的最后更新操作。根本沒有更新操作的專案將不會顯示。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/422014.html
標籤:
下一篇:AndroidStudio,Java:getCountofCursor在checkUser函式與新創建的用戶后回傳0
