sanity-io / GROQ

Specification for GROQ - The Query Language for JSON
https://groq.dev
MIT License
398 stars 15 forks source link

Proposal: add `min` and `max` functions #68

Closed coreyward closed 2 years ago

coreyward commented 3 years ago

It would be exceedingly useful to be able to both filter on and retrieve values using minimum and maximum functions.

Example 1

Fetching a value using a max function. This would be useful as an initialValue for an auto-incrementing order number field.

{
  "nextOrderNumber": max(*[_type == "order"].orderNumber) + 1
}

Example 2

Filtering using a min function to retrieve products that have a variant under $20 (e.g. for display in a collection of low-cost add-on items). For clarity, this example assumes the product document has a variants field that is an array of objects that have a priceInDollars field on them.

*[_type == "product" && min(variants.priceInDollars) < 20]
iamdagy commented 3 years ago

This would be a great addition, it would solve a common situation of auto increment order numbers for instance. 👍

judofyr commented 3 years ago

Yes, we definitely want to add min and max (together with avg and sum)!

atombender commented 2 years ago

This took a while, but we have now launched min and max functions, along with a bunch more functions!