You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-**Aggregate the current entity:** Use [aggregate functions](#aggregation-functions) like `sum` in the columns clause of your `Select` statement, usually together with [groupBy](#group-by), to summarize or group data.
630
+
631
+
-**Aggregate associated entities:** Use dedicated aggregation methods to calculate values over to-many associations directly in your queries. See [Aggregating over Associations](#aggregating-associations).
Use [aggregation functions](/guides/databases#aggregate-functions) to calculate minimums, maximums, totals, averages, and counts of values. You can use them in *columns* of `Select` statements to include the aggregated values in the result set, or in the [having](#having) clause to filter based on aggregated values.
637
+
638
+
639
+
#### Grouping { #grouping }
625
640
626
641
The Query Builder API offers a way to group the results into summarized rows (in most cases these are aggregate functions) and apply certain criteria on it.
627
642
@@ -635,7 +650,7 @@ Let's assume the following dataset for our examples:
635
650
|103 |Hugo |
636
651
|104 |Smith |
637
652
638
-
#### Group By
653
+
#####Group By { #group-by }
639
654
640
655
The `groupBy` clause groups by one or more elements and usually involves aggregate [functions](query-api#scalar-functions), such as `count`, `countDistinct`, `sum`, `max`, `avg`, and so on. It returns one row for each group.
641
656
@@ -658,7 +673,7 @@ If we execute the query on our dataset, we get the following result:
658
673
|Hugo |1 |
659
674
660
675
661
-
#### Having
676
+
#####Having { #having }
662
677
663
678
To filter the [grouped](#group-by) result, `having` is used. Both, `having` and `where`, filter the result before `group by` is applied and can be used in the same query.
664
679
@@ -678,6 +693,65 @@ If we execute the query on our dataset, we get the following result:
678
693
|Smith |3 |
679
694
680
695
696
+
#### Aggregating over Associations <Beta /> { #aggregating-associations }
697
+
698
+
Use the aggregation methods `min`, `max`, `sum`, and `count` to calculate minimums, maximums, totals, and counts of values of associated entities directly in your CQL queries. You can use these aggregation methods in *columns* to include the aggregated values in the result set, or in the *where* clause to filter the result set based on aggregated values.
699
+
700
+
::: tip
701
+
Use [infix filters](/cds/cql#with-infix-filters) to aggregate only a subset of a (to-many) association.
702
+
:::
703
+
704
+
##### min
705
+
706
+
Find the minimum value of an element in a collection.
707
+
708
+
```java
709
+
Select.from(ORDERS).columns(
710
+
o -> o.id(),
711
+
o -> o.items()
712
+
.filter(i -> i.amount().gt(0)) // optional filter
713
+
.min(i -> i.amount()).as("minAmount")
714
+
);
715
+
```
716
+
717
+
This query selects each order’s id and the minimum item amount greater than 0 as "minAmount".
718
+
719
+
##### max
720
+
721
+
Find the maximum value of an element in a collection.
This query selects all orders where at least one item has a discount.
753
+
754
+
681
755
### Ordering and Pagination
682
756
683
757
The Query Builder API allows to specify the sort order of query results. The _sort specification_ governs, according to which elements the result is sorted, and which sort order (ascending or descending) is applied.
0 commit comments