actually, use GREATEST, not LEAST, since you wanted most recent. sorry, first post was for oldest
decode(greatest(t1,t2,t3,t
Main Topics
Browse All TopicsHi There,
as an example I have timestamp columns in my table: t1, t2, t3 & t4 and 4 varchar columns in my table v1, v2, v3 & v4. I need to compare all four timestamp columns in work out which is the most recent. if t1 is the most recent then i want to select t1 & v1 as time and value. if t2 is the most recent i want to select t2 & v2 as time and value, if t3 is the most recent i want to select t3 & v3 as time and value and so on.
I was thinking to use a decode or case statement though i'm not sure how to perform the comparisons?
Can you help me write some sample sql which will fulfill this requirement.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The full statement then would be something like this:
select greatest(t1,t2,t3,t4) "Time", decode(greatest(t1,t2,t3,t
from {your_table]
where...
Note that (depending on which application you use for this) you may need to set your NLS_DATE_FORMAT manually if you want to see the actual time-of-day for the timestamp.
Also, it looks like your table structure is not ideal for answering this question with the data in an Oracle table. Whenever you use table structures in Oracle that do not contain normalized data, the queries and updates become much more complex.
after looking and playing with the code i have noticed that is one of the values is blank/null, then it displays the null and not the most recent timestamp. How should i ammend the code?
ie. in my example below fcm_tsp_prv_app_mk is null as a consequence last update time is null.
greatest( fcm_tsp_cur_app_mk, fpp_tsp_cur_trd_mk, fcm_tsp_prv_app_mk, fpp_tsp_prv_trd_mk) as last_update_time,
wrap each nullable value in an NVL that sets to some long ago default date.
Like January 1, 1900 (go back earlier if necessary)
greatest(
NVL(fcm_tsp_cur_app_mk, TO_DATE('19000101','yyyymm
NVL(fcm_tsp_cur_trd_mk, TO_DATE('19000101','yyyymm
NVL(fcm_tsp_prv_app_mk, TO_DATE('19000101','yyyymm
NVL(fpp_tsp_prv_trd_mk, TO_DATE('19000101','yyyymm
)
Business Accounts
Answer for Membership
by: sdstuberPosted on 2008-01-22 at 10:59:25ID: 20717088
decode(least(t1,t2,t3,t4), t1,v1,t2,v 2,t3,v3,t4 ,v4)