MIN_BY
Description
The MIN_BY function is used to find the value of expr1 associated with the minimum value in expr2 from a set of data. This function is very useful when dealing with paired data and can help you quickly find the best match for specific conditions.
Parameter Description
- expr1: Any data type. This is the value you want to return based on the minimum value of expr2.
- expr2: Comparable data types, including numeric types (such as tinyint, smallint, int, bigint, float, double, decimal), time types (such as date, timestamp), string types (such as char, varchar, string), and binary types (such as binary).
Return Result
- The type of the return result matches the type of expr1.
- If all values in expr2 are null, the return result is also null.
Usage Example
- Example of numeric types:
In this example, we can see that the minimum value of num2 is 5, and the value of num1 associated with it is 3.
- Example of time type:
In this example, we can see that the minimum value of date2 is '2022-01-01', and the value of the associated date1 is also '2022-01-01'.
- Example of string type:
In this example, we can see that the minimum value of str2 is 'A', and the value of str1 associated with it is 'apple'.
Notes
- Please ensure that the data types of expr1 and expr2 are compatible, otherwise the function may fail to execute.
- If all values in expr2 are null, the function will return null.
- The MIN_BY function is suitable for comparing and filtering paired data, but not for individual data items.