FOR_MAX()
Overview
FOR_MAX()
function is used to search for a maximum in a specific column and return a value related to that maximum from another column.
Syntax
Arguments
metric
: must be one of the following data types:INT
,LONG
,FLOAT
,DOUBLE
,DATE
orTIMESTAMP
value
: can be any data type exceptTEXT
The FOR_MAX()
function returns NULL
in the following situations:
- There are no input rows
- The
metric
column contains onlyNULL
values - The
value
corresponding to the metric minimum value isNULL
This function also returns NaN
(not-a-number) if the input contains a NaN
.
Examples
For the needs of this section, we will use a payment
table that stores customer payment records, including any applied discounts:
To view the payments
table content, run the following query:
FOR_MAX()
basic usage
To determine the price, with which is associated the highest discount we need to run the following code:
This query returns the following output:
FOR_MAX()
with GROUP BY
clause
In this example, we will use a GROUP BY
clause to group customers and then utilise the FOR_MAX()
function to get a discount for the highest price paid by each customer:
This query returns the following output: