FOR_MIN()
Overview
The FOR_MIN()
function is used to search for a minimum in a specific column and return a value related to that minimum 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_MIN()
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_MIN()
basic usage
To determine the price associated with the lowest discount applied across all payments, use the following query:
This query returns the following output:
FOR_MIN()
with GROUP BY
clause
To determine the discount associated with the lowest price paid by each customer, we will use the GROUP BY
clause with FOR_MIN()
function:
This query returns the following output: