Re: [SQL] Aggregates and Views

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Mark Volpe <volpe(dot)mark(at)epamail(dot)epa(dot)gov>
Cc: pgsql-sql(at)postgreSQL(dot)org
Subject: Re: [SQL] Aggregates and Views
Date: 2000-01-14 22:26:16
Message-ID: 1304.947888776@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Mark Volpe <volpe(dot)mark(at)epamail(dot)epa(dot)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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Tom Lane 2000-01-14 23:09:14 Re: [SQL] numeric question..
Previous Message Mitch Vincent 2000-01-14 21:39:05 Re: [SQL] numeric question..