简体   繁体   中英

Rank Over Partition By in Oracle SQL (Oracle 11g)

I have 4 columns in a table

  • Company Part Number
  • Manufacturer Part Number
  • Order Number
  • Part Receipt Date

    Ex.

在此处输入图片说明

I just want to return one record based on the maximum Part Receipt Date which would be the first row in the table (The one with Part Receipt date 03/31/2015).

I tried

RANK() OVER (PARTITION BY Company Part Number,Manufacturer Part Number 
            ORDER BY Part Receipt Date DESC,Order Number DESC) = 1

at the end of the WHERE statement and this did not work.

This would seem to do what you want:

select t.*
from (select t.*
      from t
      order by partreceiptdate desc
     ) t
where rownum = 1;

Analytic functions like rank() are available in the SELECT clause, they can't be invoked directly in a WHERE clause. To use rank() the way you want it, you must declare it in a subquery and then use it in the WHERE clause in the outer query. Something like this:

select company_part_number, manufacturer_part_number, order_number, part_receipt_date
from   ( select t.*, rank() over (partition by...  order by...) as rnk
         from   your_table t
       )
where  rnk = 1

Note also that you can't have a column name like company part number (with spaces in it) - at least not unless they are enclosed in double-quotes, which is a very poor practice, best avoided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM