pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Why is it that “aggregate functions are not allowed in FROM clause of their own query level”?
Date: Wed, 13 May 2020 10:57:30 -0400
Message-ID: <959.1589381850@sss.pgh.pa.us> (raw)
In-Reply-To: <20200513110251.GC24083@cloudinit-builder>
References: <20200513110251.GC24083@cloudinit-builder>

Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk> writes:
> A. Am I right in thinking that the two forms are equivalent?

No.  In the first case the SUM() aggregate does not use any variables
belonging to the "LATERAL (SELECT ..." query level, therefore, per
SQL standard, it is not an aggregate of that query but an aggregate
of the next query level up, whose variable(s) it does use.  And
within the structure of *that* query level, it's in the wrong place.

> A1. And am I right to presume that it's always possibly to rewrite
> more complicated examples that yield the same error to valid versions,
> just by coming up with a local name for the problematic fields?

In the particular case here, the problem is to control which query level
the aggregate is deemed to belong to.  I think the issue is less about
"can I rewrite the query" and more about "do I actually understand the
semantics this is asking for", so I'd be hesitant to let a tool think
that it can rearrange things like this.

			regards, tom lane





view thread (3+ messages)

Message-ID: <959.1589381850@sss.pgh.pa.us>
Permalink:  ../959.1589381850@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/959.1589381850@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, tom-postgresql.org@jaguarpaw.co.uk
  Subject: Re: Why is it that “aggregate functions are not allowed in FROM clause of their own query level”?
  In-Reply-To: <959.1589381850@sss.pgh.pa.us>
  References: <20200513110251.GC24083@cloudinit-builder> <959.1589381850@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox