Grouping aggregate functions - Mailing list pgsql-general

From Richard Connamacher
Subject Grouping aggregate functions
Date
Msg-id 9ECBA3BE-1CDD-4C2F-A7F6-B4C1AB47D402@indieimage.com
Whole thread Raw
Responses Re: Grouping aggregate functions  (Martijn van Oosterhout <kleptog@svana.org>)
List pgsql-general
Hey all,

I'm new on this list, and have been playing with Postgres a lot this
week. (Love it, by the way.)

I've got a question, if anyone can help me out. I know how to use an
aggregate function to, say, find the lowest price ever listed for a
product. I also know how to combine that with a SELECT ... GROUP BY
statement to find, say, the lowest price reported for each month.
Now, what if I want to find the *average* of all the lowest prices
for each month? Plopping that SELECT statement inside parentheses and
inside an "avg( )" function produces an error.

What I'd love to do, and which creates an error, would be something
like:

SELECT avg( ( SELECT min(price) FROM weekly_supply_prices GROUP BY
month ) )

The error I get is: "ERROR:  more than one row returned by a subquery
used as an expression" (to state the obvious). If I don't double up
the quotes I get a syntax error.

Anyone have any idea how to do this? Or do I have to compute the
average in another program?

Thanks!
Richard

pgsql-general by date:

Previous
From: Tino Wildenhain
Date:
Subject: Re: PostgreSQL makes me lie
Next
From: Martijn van Oosterhout
Date:
Subject: Re: Grouping aggregate functions