Reputation: 169
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.
Upvotes: 5
Views: 54576
Reputation:
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.
Upvotes: 3
Reputation: 1271023
This would seem to do what you want:
select t.*
from (select t.*
from t
order by partreceiptdate desc
) t
where rownum = 1;
Upvotes: 5