From: Tom Lane <tgl@sss.pgh.pa.us>
To: Mark Volpe <volpe.mark@epamail.epa.gov>
Cc: pgsql-sql@postgreSQL.org
Subject: Re: [SQL] Aggregates and Views
Date: Fri, 14 Jan 2000 17:26:16 -0500
Message-ID: <1304.947888776@sss.pgh.pa.us> (raw)
In-Reply-To: <387F923C.3E279281@epamail.epa.gov>
References: <387F923C.3E279281@epamail.epa.gov>
Mark Volpe <volpe.mark@epamail.epa.gov> writes:
> Is it possible to use an aggregate on a view
> that contains an aggregate?
Not at present. Views are implemented by a rewriter that tries
to transform your query into another SQL query, and since aggregates
of aggregates are not possible, it doesn't work. In current sources
I actually get an error from your second example:
regression=# SELECT sum(total) FROM y;
ERROR: Aggregate function calls may not be nested
For the same sort of reason, GROUPed views don't play nice with an
outer query that specifies different grouping (ie, has a GROUP clause
of its own, or perhaps an aggregate).
What we need in order to fix this is subselects in FROM clauses;
if the rewriter could transform your query into
SELECT sum(total) FROM (SELECT n, count(*) AS total FROM x GROUP BY n)
then everything would just work (and the rewriter would get a lot
simpler, too ;-)). We don't have subselects in FROM yet, but I hope
to see them in 7.1, or 7.2 at the latest.
regards, tom lane
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-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, volpe.mark@epamail.epa.gov
Subject: Re: [SQL] Aggregates and Views
In-Reply-To: <1304.947888776@sss.pgh.pa.us>
References: <387F923C.3E279281@epamail.epa.gov> <1304.947888776@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