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?
 
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
0
 
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!
0
 
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.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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.
0
 
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)...
0
 
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.
0
 
toookiAuthor Commented:
Many thanks...

Thant worked perfectly.

great to know about NULLS LAST command.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.