Skip to main content

MIN_BY

Description

The MIN_BY function returns the associated value based on the minimum value of the specified column.

Syntax

MIN_BY(<expr1>, <expr2>)

Parameters

ParameterDescription
<expr1>The expression for the associated value, supports types: Bool, TinyInt, SmallInt, Int, BigInt, LargeInt, Float, Double, Decimal, String, Date, Datetime.
<expr2>The expression for the minimum value, supports types: Bool, TinyInt, SmallInt, Int, BigInt, LargeInt, Float, Double, Decimal, String, Date, Datetime.

Return Value

Returns the same data type as . Returns NULL if there is no valid data in the group.

Example

-- setup
create table tbl(
k1 int,
k2 int,
k3 int,
k4 int
) distributed by hash(k1) buckets 1
properties ("replication_num"="1");
insert into tbl values
(0, 3, 2, 100),
(1, 2, 3, 4),
(4, 3, 2, 1),
(3, 4, 2, 1);
select min_by(k1, k4) from tbl;
+--------------------+
| min_by(`k1`, `k4`) |
+--------------------+
| 4 |
+--------------------+
select min_by(k1, k4) from tbl where k1 is null;
+----------------+
| min_by(k1, k4) |
+----------------+
| NULL |
+----------------+