docs: Add more info about inline aggregates to Expressions guide

This commit is contained in:
Rebecca Le 2024-05-25 16:14:47 +08:00
parent 1acf690da9
commit 26c1351f1e
No known key found for this signature in database
GPG key ID: 45EC503B31710A41

View file

@ -96,13 +96,15 @@ For elixir-backed data layers, they will be a function or an MFA that will be ca
## Inline Aggregates
Aggregates can be referenced in-line, with their relationship path specified and any options provided that match the options given to `Ash.Query.Aggregate.new/4`. For example:
Aggregates such as `count`, `first`, `sum`, etc. can be referenced in-line, with their relationship path specified and any options provided that match the options given to `Ash.Query.Aggregate.new/4`. For example:
```elixir
calculate :grade, :decimal, expr(
count(answers, query: [filter: expr(correct == true)]) /
count(answers, query: [filter: expr(correct == false)])
)
calculate :latest_post_published_at, :datetime, expr(max(posts, field: :published_at))
```
The available aggregate kinds can also be seen in the `Ash.Query.Aggregate` module documentation.