Add missing count example to docs

This commit is contained in:
Simon Binder 2020-03-16 12:01:11 +01:00
parent dd74948e1a
commit 8d9b0874b9
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
1 changed files with 20 additions and 2 deletions

View File

@ -124,11 +124,29 @@ Sometimes, it's useful to count how many rows are present in a group. By using t
[table layout from the example]({{<relref "../Getting started/_index.md">}}), this
query will report how many todo entries are associated to each category:
```dart
final amountOfTodos = todos.id.count();
final query = db.select(categories).join([
innerJoin(
todos,
todos.category.equalsExp(categories.id),
useColumns: false,
)
]);
query
..addColumns([amountOfTodos])
..groupBy([categories.id]);
```
If you don't want to count duplicate values, you can use `count(distinct: true)`.
Sometimes, you only need to count values that match a condition. For that, you can
use the `filter` parameter.
use the `filter` parameter on `count`.
To count all rows (instead of a single value), you can use the top-level `countAll()`
function.
To count all rows (instead of a single value), you can use the top-level `countAll()`.
More information on how to write aggregate queries with moor's Dart api is available
[here]({{< relref "joins.md#group-by" >}})
## Mathematical functions and regexp