question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItΒ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Syntax error for simple grouping in Postgres or non-optimal sql

See original GitHub issue

From 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:

  1. Write something like: Query(as.min, as.max)
  2. 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:open
  • Created 10 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mboogerdcommented, Sep 22, 2015

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:

SELECT COUNT(T1.x), COUNT(T2.y)
FROM table1 AS T1
LEFT JOIN table2 AS T2
  ON T1.x = T2.y

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?

0reactions
szeigercommented, Sep 22, 2015

No change in 3.0 or 3.1. For example, AggregateTest.testAgregates produces the following for the last query:

   ┃           ┇ select s78.s70, s84.s72, s86.s74, s87.s76
┃   ┃           ┇ from (
┃   ┃           ┇   select count(1) as s70
┃   ┃           ┇   from "t2"
┃   ┃           ┇   where "a" = 1
┃   ┃           ┇ ) s78, (
┃   ┃           ┇   select sum("a") as s72
┃   ┃           ┇   from "t2"
┃   ┃           ┇   where "a" = 1
┃   ┃           ┇ ) s84, (
┃   ┃           ┇   select sum("b") as s74
┃   ┃           ┇   from "t2"
┃   ┃           ┇   where "a" = 1
┃   ┃           ┇ ) s86, (
┃   ┃           ┇   select avg("b") as s76
┃   ┃           ┇   from "t2"
┃   ┃           ┇   where "a" = 1
┃   ┃           ┇ ) s87

IOW, it works and is decently nice and efficient but there is still no unification.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix syntax errors on GROUP BY clause when ...
I'm converting a series of queries from MySQL to PostgreSQL to run on a particular Moodle instance. I'Β ...
Read more >
Re: ERROR: syntax error at or near "group" - PostgreSQL
single table and join this resultant with the third table and so on. 'group by' clause. The general syntax should be something like...
Read more >
MySQL 8.0 Reference Manual :: 13.2.13 SELECT Statement
The SQL standard requires that HAVING must reference only columns in the GROUP BY clause or columns used in aggregate functions. However, MySQL...
Read more >
Postgre SQL errors – common codes and messages - Paessler
There are a lot of PostgreSQL errors out there. Way too much, right? Β· PostgreSQL error β€œSyntax error at or near 'grant'” Β·...
Read more >
PostgreSQL - GROUP BY - Tutorialspoint
PostgreSQL, PGadmin, SQL Tutorial + Mini Project! ... The basic syntax of GROUP BY clause is given below. The GROUP BY clause must...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found