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

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;

You could use a CASE statement

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

As of Oracle 10.2 they introduced a GREATEST function which does what you want. There is also a LEAST function too.

Examples:

select greatest(1,2) from dual;

GREATEST(1,2)
-------------
            2

select greatest(8,6,4,2) from dual;

GREATEST(8,6,4,2)
-----------------
               8

select greatest(-1,-2) from dual;

GREATEST(-1,-2)
---------------
             -1

select greatest('A','B','CCC','D') from dual;

GREATEST('A','B','CCC','D')
---------------
              D

Tags:

Sql

Oracle

Max