Max_by bigquery

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

Leave a comment