Using MAX_BY in BigQuery
The MAX_BY function in BigQuery is used to retrieve the entire row that contains the maximum value of a specific column or expression. It is commonly used when you want to find the maximum value based on a certain condition or criteria.
Syntax:
SELECT MAX_BY(column, expression) AS max_row FROM table
The column
parameter specifies the column or expression for which you want to find the maximum. The expression
parameter is used to determine the maximum value.
Example:
Let’s say we have a table called ‘sales’ with the following columns:
OrderID | Product | Quantity | Price |
---|---|---|---|
1 | A | 10 | 100 |
2 | B | 5 | 200 |
3 | C | 8 | 150 |
To find the product with the highest price, we can use the MAX_BY function as follows:
SELECT MAX_BY(Product, Price) AS max_row FROM sales
This query will return the row with the maximum price:
max_row |
---|
B |
In this example, the ‘B’ product has the highest price of 200, so it is returned as the result.
Note that MAX_BY can be used with any column or expression in BigQuery to find the maximum value based on your specific requirements.
Read more
- Java.lang.illegalstateexception: cannot invoke setvalue on a background thread
- React link is not defined
- Received value must be an htmlelement or an svgelement. received has type: array
- Insomnia ssl connect error
- Material-ui: the `fade` color utility was renamed to `alpha` to better describe its functionality.