Skip to main content
Since 1.16.0 Return the N largest values seen by the aggregate. The values are formatted as an array in decreasing order.

Samples

Find the top 5 values from i * 13 % 10007 for i = 1 to 10000.
SELECT into_array(
    max_n(sub.val, 5))
FROM (
  SELECT (i * 13) % 10007 AS val
  FROM generate_series(1,10000) as i
) sub;
Output:
into_array
---------------------------------
{10006,10005,10004,10003,10002}

Arguments

The syntax is:
into_array (
    agg MinN
) BIGINT[] | DOUBLE PRECISION[] | TIMESTAMPTZ[]
NameTypeDefaultRequiredDescription
aggMaxN-The aggregate to return the results from. Note that the exact type here varies based on the type of data stored in the aggregate.

Returns

ColumnTypeDescription
into_arrayBIGINT[ ] | DOUBLE PRECISION[ ] | TIMESTAMPTZ[ ]The lowest values seen while creating this aggregate.