How to select max, min in one request in slick

I want to do this SELECT MAX(age), MIN(age) FROM users WHERE name = 'Rick' . The best I came up with includes 2 questions: Users.filter(_.name === 'Rick').map(_.age).max

+5
source share
1 answer

This is supported, but first you need to group it. Since you are treating the entire set as a group, group it with true , and slick will ignore it when creating SQL:

 val q = Users.filter(_.name === 'Rick').groupBy { _ => true }.map { case (_, group) => (group.map(_.age).max, group.map(_.age).min) } 

This should give you something like this:

 q.selectStatement # => select max(x2."age"), min(x2."age") from "Users" x2 

Another approach you could try is pooling.

+3
source

Source: https://habr.com/ru/post/1207341/


All Articles