Search code examples
sqloracle-databasemax

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


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.


Solution

  • You could use a CASE statement

    SELECT Total = CASE WHEN number_of_items > 0 
                   THEN total/number_of_items
                   ELSE total END
    FROM   xxx