tags:

views:

93

answers:

2

I would like to get the last date of records modified. Here is a sample simple SELECT:

SELECT
 t01.name,
 t01.last_upd date1,
 t02.last_upd date2,
 t03.last_upd date3,
 'maxof123' maxdate
FROM
  s_org_ext   t01,  
  s_org_ext_x   t02,   
  s_addr_org   t03   
WHERE
  t02.par_row_id(+)= t01.row_id and
  t03.row_id(+)= t01.pr_addr_id and
  t01.int_org_flg = 'n';

How can I get column maxdate to display the max of the three dates?

Note: no UNION or sub/nested SELECT statements ;)

+1  A: 

use a CASE statement in your SELECT clause to do something like:

CASE WHEN date1 > date2 AND date1 > date3 THEN date1 WHEN date2 > date3 THEN date2 ELSE date3 END AS maxdate

It will break out of the logic as soon as the first condition is met.

northpole
+8  A: 

Greatest (t01.last_upd, t02.last_upd date2, t03.last_upd ) as maxdate

kekekela
+1, although be careful with NULLs: `greatest(X, NULL)` is null (and in that case we expect NULLs since there is an outer join)
Vincent Malgrat
Seems that this function is not supported/installed. Do I need a special package for Oracle 10/11?
Derick
Not that I'm aware of, its been around for a while. http://www.techonthenet.com/oracle/functions/greatest.php
kekekela
Great thanks! Works like a charm!
Derick