Note that there are some explanatory texts on larger screens.

plurals
  1. POGet "latest" row after GROUP BY over multiple tables
    text
    copied!<p>I'd preferably like to first query listed below and just group by <code>stories.id</code>, but I get the following error:</p> <p><code>ERROR: column "u.first_name" must appear in the GROUP BY clause or be used in an aggregate function LINE 1: SELECT "s".*, "u"."first_name", "u"."last_name", ("i"."filen...</code></p> <p>The second query works but does not group by <code>stories.id</code> and generates the wrong results. Is it possible to select from multiple tables and not group by all of them?</p> <p>The table <code>panels</code> also has a column <code>updated_at</code>. I would like to get the newest <code>file</code> per story according to <code>panels.updated_at</code>.</p> <pre><code> SELECT "s".*, "u"."first_name", "u"."last_name", ("i"."filename" || '.' || "i"."extension") AS "file" FROM "stories" "s" LEFT JOIN "panels" "p" ON("p"."story_id" = "s"."id") LEFT JOIN "users" "u" ON("s"."user_id" = "u"."uid") LEFT JOIN "images" "i" ON ("p"."image_id" = "i"."id") WHERE "s"."complete" = false AND "s"."created_by" = 205700489 GROUP BY "s"."id", ORDER BY "s"."created_at" DESC </code></pre> <hr> <pre><code> SELECT "s".*, "u"."first_name", "u"."last_name", ("i"."filename" || '.' || "i"."extension") AS "file" FROM "stories" "s" LEFT JOIN "panels" "p" ON("p"."story_id" = "s"."id") LEFT JOIN "users" "u" ON("s"."user_id" = "u"."uid") LEFT JOIN "images" "i" ON ("p"."image_id" = "i"."id") WHERE "s"."complete" = false AND "s"."created_by" = 205700489 GROUP BY "s"."id", "u"."first_name", "u"."last_name", "i"."filename", "i"."extension" ORDER BY "s"."created_at" DESC </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload