如何从表中获取最新的'n'更新行

时间:2012-10-18 07:30:36

标签: sql oracle oracle10g top-n

  

可能重复:
  Oracle SQL - How to Retrieve highest 5 values of a column

我有一个表abc,其中我有以下列

act_id,cust_id,lastUpdatedDate,custActivity act_id是主键的位置

lastUpdatedDate存储为此客户完成的上一个活动。

我正在尝试根据lastUpdatedDate为给定的custid获取最新的10行。

我怎样才能实现它。

-vivek

3 个答案:

答案 0 :(得分:1)

您可以在Oracle中使用 ROWNUM 。点击Here获取文档

select *
from  
   ( select * 
     from your_table 
     where cust_id=<given cust_id>
     order by lastUpdatedDate desc ) 
where ROWNUM <= 10;

答案 1 :(得分:0)

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

答案 2 :(得分:0)

希望这对你有帮助 -

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;