How to calculate the maximum of two numbers in Oracle SQL select?

Peter G. picture Peter G. · Aug 19, 2010 · Viewed 76k times · Source

This should be simple and shows my SQL ignorance:

SQL> select max(1,2) from dual;
select max(1,2) from dual
       *
ERROR at line 1:
ORA-00909: invalid number of arguments

I know max is normally used for aggregates. What can I use here?

In the end, I want to use something like

select total/max(1,number_of_items) from xxx;

where number_of_items is an integer and can be 0. I want to see total also in this case.

Answer

Martin Smith picture Martin Smith · Aug 19, 2010

It looks like you're using Oracle so you can use the greatest function for this in place of max

select total/greatest(1,number_of_items) 
from xxx;