我有一個帶有訂單變更歷史的表格
cart_id date action
1 01/01/2020 3: 00 批準
1 01/01/2020 2: 00 saved
2 02/03/2020 9: 00 saved
2 02/03/2020 5: 00創建的
我需要得到一個所有購物車ID的串列,其中最新的行動被 "保存"。 所以在這種情況下,它將只回傳購物車ID 2,因為 "保存 "是最新的操作,而購物車ID 1的最新操作是 "批準"。 我真的不知道該從哪里開始,有什么辦法可以實作這個目標?
uj5u.com熱心網友回復:
你可以使用ROW_NUMBER()/code>。例如:
select cart_id
from (
select cart_id, action,
row_number() over(partitionby cart_id order by date desc) as rn
from t
) x
where rn = 1 and action = 'served';
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/314840.html
標籤:
