Re: Calculating Percentages

Поиск
Список
Период
Сортировка
От Josh Berkus
Тема Re: Calculating Percentages
Дата
Msg-id 200305140824.35328.josh@agliodbs.com
обсуждение исходный текст
Ответ на Calculating Percentages  (David Siegal <dsiegal@brave.cs.uml.edu>)
Список pgsql-sql
David,

> If I were to do:
> SELECT nationality, ((COUNT(*) * 100)/(select count(*) from member)) as
> percentage FROM member GROUP BY nationality ORDER BY nationality;
>
> would this repeatedly execute the inner query over and over?

Yes, it would
Better is:

SELECT nationality, (COUNT(*)*100/total_members) as percentage
FROM member,(SELECT COUNT(*) as total_members FROM members) tot_mem
GROUP BY nationality
ORDER BY nationality

This method runs the grand total only once.


-- 
Josh Berkus
Aglio Database Solutions
San Francisco


В списке pgsql-sql по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Calculating Percentages
Следующее
От: Richard Huxton
Дата:
Сообщение: Re: Type coercion on column in a query