?
Solved

Oralce Order by DATE field does not work

Posted on 2011-10-01
7
Medium Priority
?
497 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:toooki
  • 4
  • 3
7 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36898447
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
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 36898449
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
 

Author Comment

by:toooki
ID: 36898454
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:toooki
ID: 36898468
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
 

Author Comment

by:toooki
ID: 36898475
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
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1980 total points
ID: 36898489
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
 

Author Comment

by:toooki
ID: 36898531
Many thanks...

Thant worked perfectly.

great to know about NULLS LAST command.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
What we learned in Webroot's webinar on multi-vector protection.
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question