Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable - Mailing list pgsql-bugs

From Tom Lane
Subject Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable
Date
Msg-id 24263.1240625980@sss.pgh.pa.us
Whole thread Raw
In response to Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable  (Daniel Grace <dgrace@wingsnw.com>)
List pgsql-bugs
I wrote:
> I'm talking about the underlined SELECT, not the one inside the
> aggregate.  AFAICS this one is totally useless.

Oh, wait.  It is useless in the query as written, but now that I think
twice about what you're trying to accomplish, you do need three levels
of SELECT keywords.  Just not like that.  I think what you actually
want is

SELECT
    ...
    (SELECT GROUP_CONCAT(t.fname, '; ') FROM
      (SELECT s2.fname FROM student AS s2
       WHERE s2.id=s.id ORDER BY fname) AS t) AS students,
    ...
FROM
    student AS s

What you wrote instead is just wrong --- it would fail if there were
multiple students with the same id (can that actually happen?
Maybe there's more wrong with this query...), because what you
wrote is a scalar sub-SELECT inside an aggregate call that belongs
to the outermost query.

            regards, tom lane

pgsql-bugs by date:

Previous
From: Tom Lane
Date:
Subject: Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable
Next
From: Daniel Grace
Date:
Subject: Re: Query with aggregate over subselects fails with ERROR: plan should not reference subplan's variable