Re: finding a maximum or minimum sum - Mailing list pgsql-sql

From Tom Lane
Subject Re: finding a maximum or minimum sum
Date
Msg-id 5998.992285205@sss.pgh.pa.us
Whole thread Raw
In response to finding a maximum or minimum sum  ("Michael Richards" <michael@fastmail.ca>)
List pgsql-sql
"Michael Richards" <michael@fastmail.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


pgsql-sql by date:

Previous
From: pierre@kahuna.versions.com
Date:
Subject: Re: Table design issue....
Next
From: "Josh Berkus"
Date:
Subject: Re: Table design issue....