1

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

Community
  • 1
  • 1
Vivek
  • 10,978
  • 14
  • 48
  • 66

3 Answers3

1

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;
Joe G Joseph
  • 23,518
  • 5
  • 56
  • 58
0

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
John Woo
  • 258,903
  • 69
  • 498
  • 492
0

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;
nnnn
  • 1,041
  • 3
  • 18
  • 35