Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5BInGE39985 for ; Mon, 11 Jun 2001 14:49:16 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.3/8.11.3) with ESMTP id f5BIkj006001; Mon, 11 Jun 2001 14:46:45 -0400 (EDT) To: "Michael Richards" cc: pgsql-sql@postgresql.org Subject: Re: finding a maximum or minimum sum In-reply-to: <3B24F48D.000035.19989@frodo.searchcanada.ca> References: <3B24F48D.000035.19989@frodo.searchcanada.ca> Comments: In-reply-to "Michael Richards" message dated "Mon, 11 Jun 2001 12:40:45 -0400" Date: Mon, 11 Jun 2001 14:46:45 -0400 Message-ID: <5998.992285205@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200106/167 X-Sequence-Number: 3390 "Michael Richards" 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