1

可能重复:
Oracle SQL - 如何检索列的最高 5 个值

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

act_id,cust_id,lastUpdatedDate,custActivity. 其中act_id 是主键

lastUpdatedDate 存储为此客户完成的最后一项活动。

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

我怎样才能实现它。

-维维克

4

3 回答 3

1

您可以在 Oracle中使用ROWNUM 。单击此处获取文档

select *
from  
   ( select * 
     from your_table 
     where cust_id=<given cust_id>
     order by lastUpdatedDate desc ) 
where ROWNUM <= 10;
于 2012-10-18T07:34:10.157 回答
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
于 2012-10-18T07:33:01.870 回答
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;
于 2012-10-18T07:38:06.853 回答