Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

oracle

max

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.

like image 876
Peter G. Avatar asked Aug 19 '10 11:08

Peter G.


People also ask

How do you find the highest number in Oracle?

Oracle MAX() function syntax The Oracle MAX() function is an aggregate function that returns the maximum value of a set. Similar to the MIN() function, the DISTINCT and ALL clauses are irrelevant to the MAX() function. The Oracle MAX() function also ignores NULL values.

What is Max function Oracle?

MAX is an aggregate function that evaluates the maximum of an expression over a set of rows (see Aggregates (set functions)). MAX is allowed only on expressions that evaluate to built-in data types (including CHAR, VARCHAR, DATE, TIME, CHAR FOR BIT DATA, etc.).

How do you find the max in a query?

The SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.


1 Answers

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; 
like image 198
Martin Smith Avatar answered Oct 12 '22 22:10

Martin Smith