Re: Aggregates containing outer references don't work per

From: Jan Wieck <JanWieck(at)Yahoo(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Aggregates containing outer references don't work per
Date: 2003-06-05 01:39:50
Message-ID: 3EDE9F66.6060509@Yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Tom Lane wrote:
> Some of the Red Hat guys have been trying to work through the NIST SQL
> compliance tests. So far they've found several things we already knew
> about, and one we didn't:
>
> -- TEST:0434 GROUP BY with HAVING EXISTS-correlated set function!
> SELECT PNUM, SUM(HOURS) FROM WORKS
> GROUP BY PNUM
> HAVING EXISTS (SELECT PNAME FROM PROJ
> WHERE PROJ.PNUM = WORKS.PNUM AND
> SUM(WORKS.HOURS) > PROJ.BUDGET / 200);
>
> This query is legal according to the test, but Postgres fails with
> ERROR: Aggregates not allowed in WHERE clause
>
> The SUM() should be allowed in the sub-SELECT because, according to the
> spec, it is actually an aggregate of the outer query --- and so the
> whole expression "SUM(WORKS.HOURS)" is effectively an outer reference
> for the sub-SELECT.
> [...]
>
> Comments?

Would

SELECT PNUM, SUM(HOURS) FROM WORKS
GROUP BY PNUM
HAVING EXISTS (SELECT PNAME FROM PROJ
WHERE PROJ.PNUM = WORKS.PNUM AND
AVG(WORKS.HOURS) > PROJ.MAGIC / 200);
^^^

be legal according to that spec too? Then the parser would not only have
to identify the uplevel of the aggregate, it'd also have to add a junk
aggregate TLE to the outer TL.

Jan

--
#======================================================================#
# It's easier to get forgiveness for being wrong than for being right. #
# Let's break this rule - forgive me. #
#================================================== JanWieck(at)Yahoo(dot)com #

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Christopher Kings-Lynne 2003-06-05 02:28:47 Re: Problems with renaming a column
Previous Message Bruce Momjian 2003-06-05 01:22:47 Re: Aggregates containing outer references don't work per