Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for DISTINCT usage in aggregates #326

Closed
anddero opened this issue Jun 18, 2021 · 6 comments
Closed

Support for DISTINCT usage in aggregates #326

anddero opened this issue Jun 18, 2021 · 6 comments

Comments

@anddero
Copy link

anddero commented Jun 18, 2021

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 anddero changed the title Support for DISTINCT usage in aggregation column selection Support for DISTINCT usage in aggregates Jun 18, 2021
@gavinking
Copy link
Contributor

gavinking commented Aug 12, 2023

It's true that we do allow avg(distinct) and sum(distinct) in JPQL—and even the completely meaningless min(distinct) and max(distinct)—but how are these actually useful?

Unless someone can convince me that avg(distinct) is a meaningful and useful thing to write, I would say let's not bloat out the CriteriaBuilder interface.

@anddero
Copy link
Author

anddero commented Aug 13, 2023

I don't know what it was at the time of writing. I guess the data could have been a result of a subquery. Now I can't think of anything practical, maybe one of the upvoters can.

@gavinking
Copy link
Contributor

@beikov, @andyjefferson, @dazey3, @TomBeckett you all voted for this. Can someone describe a legit usecase for it?

@beikov
Copy link

beikov commented Aug 28, 2023

Sorry, I don't remember what it was that drove me to upvoting this ticket. Maybe consistency with JPQL? I can't think of a real usecase for this feature.

@gavinking
Copy link
Contributor

Thanks. I'm going to close it then. If someone can think of something, it can always be reopened.

@gavinking gavinking closed this as not planned Won't fix, can't repro, duplicate, stale Aug 28, 2023
@Mettbrot
Copy link

Mettbrot commented Aug 7, 2024

Hi, I have a usecase for this: We would like to aggregate all possible values that exist in a column, so we could distribute the values using powers of two which allows us to trace back to the individuals but ONLY if they are distinct before the sum.

@andyjefferson, @dazey3, @TomBeckett any of you remember your usecases?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants