From 1fd88458565968b93b4460b8a0cd8fc8fef9408d Mon Sep 17 00:00:00 2001 From: Johan Solbakken Date: Fri, 25 Sep 2026 15:10:00 +0200 Subject: [PATCH] Document argmin and argmax grouping aggregators Co-Authored-By: Claude Fable 5.1 --- en/querying/grouping.html | 7 +++++++ en/reference/querying/grouping-language.html | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/en/querying/grouping.html b/en/querying/grouping.html index 29d316006f..446a14bcfd 100644 --- a/en/querying/grouping.html +++ b/en/querying/grouping.html @@ -1842,6 +1842,13 @@

Collecting aggregates

select * from purchase where true limit 0 |
   all(group(customer) each(output(quantiles([0.5,0.9], price))))
   
+

+ Use argmin or argmax to return the value of one field for the document + with the smallest or largest value of another, for instance the item of the most expensive purchase per customer: +

+
select * from purchase where true limit 0 |
+  all(group(customer) each(output(argmax(price, item))))
+  
diff --git a/en/reference/querying/grouping-language.html b/en/reference/querying/grouping-language.html index a006d1e491..346babab39 100644 --- a/en/reference/querying/grouping-language.html +++ b/en/reference/querying/grouping-language.html @@ -325,6 +325,7 @@

Aggregators

  • The count of documents in a group
  • The sum, average, min, max, xor or standard deviation of an expression
  • Multiple quantiles of an expressions value
  • +
  • The value of an expression for the document with the smallest or largest value of another expression (argmin, argmax)
  • When all arguments are numeric, the result type is resolved by looking at the argument types. @@ -345,6 +346,16 @@

    Aggregators

    all( group(city) each(output(quantiles([0.5, 0.9], delivery_days))) )
    This computes the median (p50) and 90th percentile (p90) time to delivery in days per city. Note that quantiles are computed using KLL Sketch, so they are approximate.

    +Use argmin(key, value) and argmax(key, value) to get the value of one expression +for the document that has the smallest or largest value of another expression: +

    all( group(artist) each(output(argmin(price, title))) )
    +This outputs the title of the cheapest track per artist, where min(price) would only output the price. +The key must be numeric, while the value can be of any type, including multivalue. +Documents where the key is not set are skipped, and the output is null if no document in the group has a key. +If several documents share the smallest or largest key, one of them is selected. +Both can be used in order, e.g. order(-argmin(price, title)). +Available since {% include version.html version="8.761.27" %}. +

    Multivalue fields, such as maps and arrays, can be used for grouping. However, using aggregation functions such as sum() on such fields can give misleading results. Assume a map from strings to integers (map<string, int>), @@ -381,6 +392,8 @@

    Aggregators

    xorXOR the values (their least significant 64 bits) of all selected documentsAnyLong stddevComputes the population standard deviation over all selected documentsNumericDouble quantilesComputes one or multiple quantiles of the values of an expression. Quantiles must be a number between 0 and 1 inclusive.[Numeric+], Expr[{"quantile":Double,"value":Double}+] + argminKeeps the value of the second argument for the document with the smallest value of the first argumentNumeric, AnyAny + argmaxKeeps the value of the second argument for the document with the largest value of the first argumentNumeric, AnyAny

    Hit aggregators

    NameDescriptionArgumentsResult @@ -638,6 +651,8 @@

    Grouping language grammar

    ( "xor" "(" exp ")" ) | ( "stddev" "(" exp ")" ) | ( "quantiles" "(" "[" number ( "," number )* "]" "," exp ")" ) | + ( "argmin" "(" exp "," exp ")" ) | + ( "argmax" "(" exp "," exp ")" ) | ( "summary" "(" [ identifier ] ")" ) ) [ "as" "(" identifier ")" ] expList ::= exp ( "," exp )*