- What are the N smallest or largest values in my dataset?
- Which rows contain the minimum or maximum values?
- How can I efficiently track top/bottom values over time?
min_n(): Get the N smallest values from a columnmax_n(): Get the N largest values from a columnmin_n_by(): Get the N smallest values with accompanying data (like full rows)max_n_by(): Get the N largest values with accompanying data (like full rows)
SELECT ... ORDER BY ... LIMIT n. But unlike the SQL query, they can be composed
and combined like other aggregate hyperfunctions.
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:- More efficient because multiple accessors can reuse the same aggregate
- Easier to reason about performance, because aggregation is separate from final computation
- Easier to understand when calculations can be rolled up into larger intervals, especially in window functions and continuous aggregates
- Perform retrospective analysis even when underlying data is dropped, because the intermediate aggregate stores extra information not available in the final result
Samples
Find the smallest values
Get the 5 smallest values from a calculation. This example usesmin_n() to
find the bottom 5 values from i * 13 % 10007 for i = 1 to 10000:
Find the largest values
Get the 5 largest values from a calculation. This example usesmax_n() to
find the top 5 values from i * 13 % 10007 for i = 1 to 10000:
Find the smallest transactions with details
This example assumes you have a table of stock trades:min_n_by() to track both the transaction size and
associated row data:
Find the largest transactions with details
Find the 10 largest transactions each day. This example usesmax_n_by():
Available functions
Minimum values
min_n(): get the N smallest values from a column
Maximum values
max_n(): get the N largest values from a column
Minimum values with data
min_n_by(): get the N smallest values with accompanying data
Maximum values with data
max_n_by(): get the N largest values with accompanying data