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 @@
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 @@
When all arguments are numeric, the result type is resolved by looking at the argument types. @@ -345,6 +346,16 @@
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 @@