Re: finding a maximum or minimum sum

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Michael Richards" <michael(at)fastmail(dot)ca>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: finding a maximum or minimum sum
Date: 2001-06-11 18:46:45
Message-ID: 5998.992285205@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

"Michael Richards" <michael(at)fastmail(dot)ca> writes:
> I run a select sum(amount) from payments group by userid
> I need to modify this query so it returns the minimum, maximum and
> average sums. Is there any way I can do this?

You need two levels of grouping/aggregating to make that happen.
In 7.1 you can do it directly:

select min(amtsum), max(amtsum), avg(amtsum)
from (select sum(amount) as amtsum from payments group by userid) ss;

In prior versions you'd need to do the initial select into a temp
table and then select min/max/avg from that.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Michael Richards 2001-06-11 19:03:29 Re: finding a maximum or minimum sum
Previous Message Michael Richards 2001-06-11 18:22:01 Re: finding a maximum or minimum sum