可能重複:
Oracle SQL - How to Retrieve highest 5 values of a column如何從表中獲取最新的 'N' 更新的行
我有一個表ABC,在那裏我有以下幾列
act_id,cust_id,lastUpdatedDate,custActivity
。其中act_id是主鍵。
lastUpdatedDate存儲爲此客戶完成的上次活動。
我想根據lastUpdatedDate得到最新的10行給定的custid。
我該如何實現它。
-vivek
可能重複:
Oracle SQL - How to Retrieve highest 5 values of a column如何從表中獲取最新的 'N' 更新的行
我有一個表ABC,在那裏我有以下幾列
act_id,cust_id,lastUpdatedDate,custActivity
。其中act_id是主鍵。
lastUpdatedDate存儲爲此客戶完成的上次活動。
我想根據lastUpdatedDate得到最新的10行給定的custid。
我該如何實現它。
-vivek
您可以使用ROWNUM在Oracle中。點擊Here的文檔
select *
from
(select *
from your_table
where cust_id=<given cust_id>
order by lastUpdatedDate desc)
where ROWNUM <= 10;
Oracle支持ROW_NUMBER()
和窗函數。請嘗試以下,
SELECT act_id, cust_id, lastUpdatedDate, custActivity
FROM
(
SELECT act_id, cust_id, lastUpdatedDate, custActivity,
ROW_NUMBER() OVER (PARTITION BY cust_id ORDER BY lastUpdatedDate DESC) rn
FROM tableNAME
) x
WHERE rn <= 10
希望這會有助於你 -
select act_id,cust_id,lastUpdatedDate,custActivity
from (
select act_id,cust_id,lastUpdatedDate,custActivity, row_number() over (order by lastUpdatedDate) r
from abc
where act_id=<cust_id>
)
where r between 1 and 10;