YABADABADOU
YABADABADOU

Reputation: 1328

Is there a function to get the max of two values in Google BigQuery?

I want to get the maximum value of 2 Integer (or 2 float).

I know I can do it with a IF function like this: IF (column1 > column2, column1, column2)

however I was wondering if a function to do that exists or if there is a plan to add that kind of function in the future.

In MySQL there is the GREATER function that can do that. Example: GREATER(column1, column2).

Upvotes: 39

Views: 56141

Answers (2)

Jason Roselander
Jason Roselander

Reputation: 836

BigQuery supports

GREATEST(expr1, expr2, ...) 

which returns the largest argument. I've filed an internal bug to get this added to our public documentation.

Upvotes: 81

Jordan Tigani
Jordan Tigani

Reputation: 26617

There isn't currently a function to return the greater of two values in BigQuery. If you end up needing to compute the value a lot in a single query, you can always get the greater value in a subselect.

For example:

SELECT gr 
FROM (
    SELECT IF(column1 > column2, column1, column2) as gr 
    FROM [my_dataset.my_table])
WHERE gr > 27
GROUP BY gr

Upvotes: 3

Related Questions