Oralce Order by DATE field does not work

I have a table MyTAB in Oracle 11gR2 with rows:

F1      F2                          F3
-----------------------------------
x        7/8/2011              100
x        7/27/2011            200
x                                       300
y                                       400
y                                       600

Need O/P:
F1      F2                          F3
-----------------------------------
x        7/27/2011            200
y                                       600

Here F1 is varchar2, F2 is DATE and F3 is number.

I need to get max(F2) i.e. the latest F2 value for a given F1 and the corresponding record from above. If there is at least one non-null F2 value for a given F1, I ignore any null F2 row for that given F1 (in the above case ignore row 3 with F3=300)
And if "all" F2 values are null then  I need to choose the record with max F3.

I tried as in attached code by with date is not coming in ascending order. 7/27/2011 seems lower than 7/8/2011 . Also I cannot choose the largest F3 value when all F2 values are null.



 
select * from 
(SELECT r.*, ROW_NUMBER() OVER(PARTITION BY r.F1 ORDER BY r.F2 ASC) rn                                                                 
from MyTAB r)
where rn = 1

Open in new window

toookiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
If you want MAX date, then you should do DESC.

select * from
(SELECT r.*, ROW_NUMBER() OVER(PARTITION BY r.F1 ORDER BY r.F2 DESC) rn                                                                
from MyTAB r)
where rn = 1

Additionally, in your example, you have empty F2 values. Even if not blank, it appears if there is a tie, you want the highest F3. If so, then change to this:

select * from
(SELECT r.*, ROW_NUMBER() OVER(PARTITION BY r.F1 ORDER BY r.F2 DESC, r.F3 DESC) rn                                                                
from MyTAB r)
where rn = 1

Hope that helps!
Kevin CrossChief Technology OfficerCommented:
As a note, I believe NULLs automatically sort LAST in Oracle as it does in MS SQL, but if not note that Oracle has capability to specify sort order of NULLs explicitly in the OVER(ORDER BY) clause.
toookiAuthor Commented:
Thank you.
If F2 is null, it comes first with rn=1 when we do:

select * from
(SELECT r.*, ROW_NUMBER() OVER(PARTITION BY r.F1 ORDER BY r.F2 DESC, r.F3 DESC) rn                                                                
from MyTAB r)
where rn = 1

So in the above case,
x                                       300
records gets picked up.
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

toookiAuthor Commented:
Also if I do order by r.F1 ORDER BY r.F2 DESC, r.F3 DESC
Then any NULL F3 value comes in the pace of rn=1. (F3 is number but NULL seems bigger than 100 or so)...
toookiAuthor Commented:
Ok, I changed a bit:
ORDER BY NVL(r.F2, to_date('01/01/1900', 'mm/dd/yyyy')) DESC, NVL(r.F3, -9999) DESC)

Seemed to worked for few...checking.
Kevin CrossChief Technology OfficerCommented:
Did you see my note on NULLS LAST capabilities of Oracle?

select * from
(SELECT r.*, ROW_NUMBER() OVER(PARTITION BY r.F1 ORDER BY r.F2 DESC NULLS LAST, r.F3 DESC NULLS LAST) rn                                                                
from MyTAB r)
where rn = 1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
toookiAuthor Commented:
Many thanks...

Thant worked perfectly.

great to know about NULLS LAST command.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.