| From: | Martijn van Oosterhout <kleptog(at)svana(dot)org> |
|---|---|
| To: | Richard Connamacher <rich(dot)n1(at)indieimage(dot)com> |
| Cc: | pgsql-general(at)postgresql(dot)org |
| Subject: | Re: Grouping aggregate functions |
| Date: | 2006-04-02 11:51:30 |
| Message-ID: | 20060402115130.GA1298@svana.org |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
On Sun, Apr 02, 2006 at 04:03:03AM -0700, Richard Connamacher wrote:
> 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.
Use a subquery. ie.e not:
> SELECT avg( ( SELECT min(price) FROM weekly_supply_prices GROUP BY
> month ) )
But
SELECT avg(minprice) FROM
(SELECT min(price) as minprice FROM weekly_supply_prices GROUP BY month );
> Anyone have any idea how to do this? Or do I have to compute the
> average in another program?
Use SQL to calculate both :) One way to think about it is by think of
the subquery producing a temporary table which you then use in another
query.
Have a nice day,
--
Martijn van Oosterhout <kleptog(at)svana(dot)org> http://svana.org/kleptog/
> Patent. n. Genius is 5% inspiration and 95% perspiration. A patent is a
> tool for doing 5% of the work and then sitting around waiting for someone
> else to do the other 95% so you can sue them.
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Tom Lane | 2006-04-02 16:35:18 | Re: installation problem - semaphores |
| Previous Message | fufay | 2006-04-02 11:37:44 | Re: installation problem - semaphores |