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().
This function group uses the two-step aggregation
pattern. In addition to the usual aggregate function min_n_by, 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:- 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
This example assumes that you have a table of stock trades in this format:Available functions
Aggregate
min_n_by(): construct an aggregate that keeps track of the smallest values and associated data
Accessors
into_values(): return the N lowest values with their associated data
Rollup
rollup(): combine multiple MinNBy aggregates