pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Michael Richards <michael@fastmail.ca>
Cc: pgsql-sql@postgresql.org
Subject: Re: finding a maximum or minimum sum
Date: Mon, 11 Jun 2001 14:46:45 -0400
Message-ID: <5998.992285205@sss.pgh.pa.us> (raw)
In-Reply-To: <3B24F48D.000035.19989@frodo.searchcanada.ca>
References: <3B24F48D.000035.19989@frodo.searchcanada.ca>

"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



view thread (5+ messages)  latest in thread

Message-ID: <5998.992285205@sss.pgh.pa.us>
Permalink:  ../5998.992285205@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/5998.992285205@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: tgl@sss.pgh.pa.us, michael@fastmail.ca
  Subject: Re: finding a maximum or minimum sum
  In-Reply-To: <5998.992285205@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox