Search code examples
jpabucket

jpa query to create intervals / buckets for age


If i have an entity person with date of birth or age, how to create a query that returns number of persons in user defined age intervals e.g if user provides 10 as interval then we want to see

1-10 3 10-20 5 20-30 6

etc ...

Already trying some queries but they are database dependent like specific to mysql.


Solution

  • select (u.age / 10), count(u.id) from User u group by (u.age / 10)