persistence
persistence copied to clipboard
Support for `DISTINCT` usage in aggregates
Without DISTINCT
For a query like
SELECT
SUM(my_table.my_col)
FROM
-- ...
you can write the equivalent CriteriaQuery
// ... previously defined CriteriaBuilder builder, CriteriaQuery query, Root<MyTable> root
query.select(builder.sum(root.get("myCol")));
// ...
With DISTINCT
But for a query like
SELECT
SUM(DISTINCT my_table.my_col)
FROM
-- ...
I suggest adding the following method to CriteriaBuilder
<X> Expression<X> distinct(Expression<X> x)
so we could write
// ... previously defined CriteriaBuilder builder, CriteriaQuery query, Root<MyTable> root
query.select(builder.sum(builder.distinct(root.get("myCol"))));
// ...
as currently there does not seem to be a way to achieve this. A countDistinct
method exists, but what about DISTINCT
for other aggregates?