Introduction

Get the N smallest values from a column, with an associated piece of data per value. For example, you can return an accompanying column, or the full row.

The min_n_by() functions give the same results as the regular SQL query SELECT ... ORDER BY ... LIMIT n. But unlike the SQL query, they can be composed and combined like other aggregate hyperfunctions.

To get the N largest values with accompanying data, use max_n_by(). To get the N smallest values without accompanying data, use min_n().

Related hyperfunction groups

Aggregate

min_n_by
Track the smallest values and associated data in a set of values

Accessor

into_values
Returns the lowest values and associated data from a MinNBy aggregate

Rollup

rollup
Combine multiple MinNBy aggregates
min_n_by(
value BIGINT | DOUBLE PRECISION | TIMESTAMPTZ,
data ANYELEMENT,
capacity BIGINT
) MinNBy

Construct an aggregate that keeps track of the smallest values passed through it, as well as some associated data which is passed alongside the value.

Required arguments
NameTypeDescription
valueBIGINT, DOUBLE PRECISION, TIMESTAMPTZThe values passed into the aggregate
dataANYELEMENTThe data associated with a particular value
capacityBIGINTThe number of values to retain.
Returns
ColumnTypeDescription
min_n_byMinNByThe compiled aggregate. Note that the exact type is MinByInts, MinByFloats, or MinByTimes depending on the input type
into_values(
agg MinNBy,
dummy ANYELEMENT
) TABLE (
value BIGINT | DOUBLE PRECISION | TIMESTAMPTZ, data ANYELEMENT
)

This returns the smallest values seen by the aggregate and the corresponding values associated with them. Note that PostgresQL requires an input argument with type matching the associated value in order to deterimine the response type.

Required arguments
NameTypeDescription
aggMinNByThe aggregate to return the results from. Note that the exact type here varies based on the type of data stored.
dummyANYELEMENTThis is purely to inform PostgresQL of the response type. A NULL cast to the appropriate type is typical.
Returns
ColumnTypeDescription
into_valuesTABLE (value BIGINT, DOUBLE PRECISION, TIMESTAMPTZ, data ANYELEMENT)The smallest values and associated data seen while creating this aggregate.
Examples

Find the bottom 5 values from i * 13 % 10007 for i = 1 to 10000, and the integer result of the operation that generated that modulus:

SELECT into_values(
min_n_by(sub.mod, sub.div, 5),
NULL::INT)
FROM (
SELECT (i * 13) % 10007 AS mod, (i * 13) / 10007 AS div
FROM generate_series(1,10000) as i
) sub;
into_values
-------------
(1,9)
(2,5)
(3,1)
(4,10)
(5,6)
rollup(
agg MinNBy
) MinNBy

This aggregate combines the aggregates generated by other minnby aggregates and returns the minimum values and associated data found across all the aggregated data.

Required arguments
NameTypeDescription
aggMinNByThe aggregates being combined
Returns
ColumnTypeDescription
rollupMinNByAn aggregate over all of the contributing values.

This example assumes that you have a table of stock trades in this format:

CREATE TABLE stock_sales(
ts TIMESTAMPTZ,
symbol TEXT,
price FLOAT,
volume INT
);

Find the 10 smallest transactions in the table, what time they occurred, and what symbol was being traded.

SELECT
(data).time,
(data).symbol,
value AS transaction
FROM
into_values((
SELECT min_n_by(price * volume, stock_sales, 10)
FROM stock_sales
),
NULL::stock_sales);

Keywords

Found an issue on this page?

Report an issue!