Syntax error for simple grouping in Postgres or non-optimal sql
See original GitHub issueFrom http://stackoverflow.com/questions/21414449/aggregate-multiple-columns-without-groupby-in-slick-2-0:
I would like to perform an aggregation with Slick that executes SQL like the following:
SELECT MIN(a), MAX(a) FROM table_a;
where table_a
has an INT
column a
In Slick given the table definition:
class A(tag: Tag) extends Table[Int](tag, "table_a") {
def a = column[Int]("a")
def * = a
}
val A = TableQuery[A]
val as = A.map(_.a)
It seems like I have 2 options:
- Write something like:
Query(as.min, as.max)
- Write something like:
as
.groupBy(_ => 1)
.map { case (_, as) => (as.map(identity).min, as.map(identity).max) }
However, the generated sql is not good in either case. In 1, there are two separate sub-selects generated, which is like writing two separate queries. In 2, the following is generated:
select min(x2."a"), max(x2."a") from "table_a" x2 group by 1
However, this syntax is not correct for Postgres (it groups by the first column value, which is invalid in this case). Indeed AFAIK it is not possible to group by a constant value in Postgres, except by omitting the group by clause.
Is there a way to cause Slick to emit a single query with both aggregates without the GROUP BY
?
Issue Analytics
- State:
- Created 10 years ago
- Comments:7 (6 by maintainers)
Out of curiosity, what is the status of this issue (w.r.t. Slick 3.X?). I have found myself wanting to express a similarly simple statement:
I managed to get it working with groupBy, but couldnβt figure out how to do it without. I found another related issue on SO about this (http://stackoverflow.com/questions/29278685/how-to-make-multiple-aggregations-with-slick-without-group-by), but no answers.
Is this aggregation expressible in Slick 3 without groupBy and have I just missed how? Or is it unresolved for both Slick 2 and 3?
If it isnβt resolved, am I correct in assuming a native query would be the way to go if efficiency matters?
No change in 3.0 or 3.1. For example, AggregateTest.testAgregates produces the following for the last query:
IOW, it works and is decently nice and efficient but there is still no unification.