Possible Duplicate:
Oracle SQL - How to Retrieve highest 5 values of a column

i have a table abc, where i have following columns

act_id,cust_id,lastUpdatedDate,custActivity. Where act_id is primary key .

lastUpdatedDate store last activity done for this customer.

i am trying to get latest 10 rows for given custid based on lastUpdatedDate.

How can i achieve it.

-vivek

有帮助吗?

解决方案

You can use ROWNUM in Oracle.. Click Here for Documentation

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

其他提示

Oracle supports ROW_NUMBER() and window function. Try the following,

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

Hope this may helpful to you-

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;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top