Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query one row with max value in one column in Slick

It seems to me like a simple problem, but I still try to find a good solution. I am using Slick 3.0. I want to query the row of a table, which has the highest value in one column. But I don't want to only have the highest value (this is simple), I want to have the whole row. I tried some things, like query first the max and then filter with this max value, but nothing compiled or looked appropiate. I would expect to be there a method like that:

table.maxBy(_.columnName)

But I didn't found a method like that. So what's the favorite way to do something like that?

like image 316
F. Böller Avatar asked Jul 04 '15 16:07

F. Böller


1 Answers

The way to do it is to use this query:

table.sortBy(_.columnName).take(1).result

Unfortunately it produces SQL that is not optimized (but correct). Issue is reported and fixed, it'll be released in 3.1.0.

like image 95
sap1ens Avatar answered Sep 19 '22 10:09

sap1ens