Re: finding a maximum or minimum sum

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: finding a maximum or minimum sum
Дата
Msg-id 5998.992285205@sss.pgh.pa.us
обсуждение исходный текст
Ответ на finding a maximum or minimum sum  ("Michael Richards" <michael@fastmail.ca>)
Список 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 по дате отправления:

Предыдущее
От: pierre@kahuna.versions.com
Дата:
Сообщение: Re: Table design issue....
Следующее
От: "Josh Berkus"
Дата:
Сообщение: Re: Table design issue....