> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify-poc.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Minimum values overview

> Get the N smallest values from a column

Get the N smallest values from a column.

The `min_n()` 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, use [`max_n()`][max_n]. To get the N smallest
values with accompanying data, use [`min_n_by()`][min_n_by].

This function group uses the [two-step aggregation][two-step-aggregation]
pattern. In addition to the usual aggregate function [`min_n`][min_n], it also
includes accessors and rollup functions.

## Two-step aggregation

This group of functions uses the two-step aggregation pattern.

Rather than calculating the final result in one step, you first create an
intermediate aggregate by using the aggregate function.

Then, use any of the accessors on the intermediate aggregate to calculate a
final result. You can also roll up multiple intermediate aggregates with the
rollup functions.

The two-step aggregation pattern has several advantages:

1. More efficient because multiple accessors can reuse the same aggregate
2. Easier to reason about performance, because aggregation is separate from
   final computation
3. Easier to understand when calculations can be rolled up into larger
   intervals, especially in window functions and continuous aggregates
4. Perform retrospective analysis even when underlying data is dropped, because
   the intermediate aggregate stores extra information not available in the
   final result

To learn more, see the [blog post on two-step aggregates][blog-two-step-aggregates].

[blog-two-step-aggregates]: https://www.timescale.com/blog/how-postgresql-aggregation-works-and-how-it-inspired-our-hyperfunctions-design

## Samples

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

```sql theme={"dark"}
CREATE TABLE stock_sales(
    ts TIMESTAMPTZ,
    symbol TEXT,
    price FLOAT,
    volume INT
);
```

You can query for the 10 smallest transactions each day:

```sql theme={"dark"}
WITH t as (
    SELECT
        time_bucket('1 day'::interval, ts) as day,
        min_n(price * volume, 10) AS daily_min
    FROM stock_sales
    GROUP BY time_bucket('1 day'::interval, ts)
)
SELECT
    day, into_array(daily_min)
FROM t;
```

## Available functions

### Aggregate

* [`min_n()`][min_n]: construct an aggregate that keeps track of the smallest values passed through it

### Accessors

* [`into_values()`][into_values]: return the N lowest values seen by the aggregate
* [`into_array()`][into_array]: return the N lowest values seen by the aggregate as an array

### Rollup

* [`rollup()`][rollup]: combine multiple MinN aggregates

[into_array]: /api-reference/timescaledb-toolkit/minimum-and-maximum/min_n/into_array

[into_values]: /api-reference/timescaledb-toolkit/minimum-and-maximum/min_n/into_values

[max_n]: /api-reference/timescaledb-toolkit/minimum-and-maximum/max_n/max_n

[min_n]: /api-reference/timescaledb-toolkit/minimum-and-maximum/min_n/min_n

[min_n_by]: /api-reference/timescaledb-toolkit/minimum-and-maximum/min_n_by/min_n_by

[rollup]: /api-reference/timescaledb-toolkit/minimum-and-maximum/min_n/rollup

[two-step-aggregation]: #two-step-aggregation
