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,DATEorTIMESTAMPvalue: can be any data type exceptTEXT
FOR_MAX() function returns NULL in the following situations:
- There are no input rows
- The
metriccolumn contains onlyNULLvalues - The
valuecorresponding to the metric minimum value isNULL
NaN (not-a-number) if the input contains a NaN.
Examples
For the needs of this section, we will use apayment table that stores customer payment records, including any applied discounts:
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:
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: