persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Support for `DISTINCT` usage in aggregates

Open anddero opened this issue 3 years ago • 0 comments

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?

anddero avatar Jun 18 '21 09:06 anddero