Re: Calculating Percentages - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Calculating Percentages
Date
Msg-id 200305140824.35328.josh@agliodbs.com
Whole thread Raw
In response to Calculating Percentages  (David Siegal <dsiegal@brave.cs.uml.edu>)
List 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 by date:

Previous
From: Tom Lane
Date:
Subject: Re: Calculating Percentages
Next
From: Richard Huxton
Date:
Subject: Re: Type coercion on column in a query