kungcc
kungcc

Reputation: 1852

JPA Select only one of each value in one column

I have a list like:

Name       Age
Charles    18
Anna       20
Anna       19
Tomas      44
Karla      13
Charles    88

I would write a JPQL statement that give me:

Charles    18
Anna       20
Tomas      44
Karla      13

In other words, how can I get a list with unique names where the age dont care?

Best regards

Carl

Upvotes: 0

Views: 1135

Answers (1)

JB Nizet
JB Nizet

Reputation: 691735

If you really don't care about the age, don't select it:

select distinct u.name from User u

If you'd like to get a valid age with each user, but don't care which one, select the min or max of the ages:

select u.name, max(u.age) from User u group by u.name

Upvotes: 4

Related Questions