Re: [SQL] Aggregates and Views - Mailing list pgsql-sql

From Tom Lane
Subject Re: [SQL] Aggregates and Views
Date
Msg-id 1304.947888776@sss.pgh.pa.us
Whole thread Raw
In response to Aggregates and Views  (Mark Volpe <volpe.mark@epamail.epa.gov>)
List pgsql-sql
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


pgsql-sql by date:

Previous
From: "Mitch Vincent"
Date:
Subject: Re: [SQL] numeric question..
Next
From: Tom Lane
Date:
Subject: Re: [SQL] numeric question..