Re: Calculating Percentages

From: Josh Berkus <josh(at)agliodbs(dot)com>
To: David Siegal <dsiegal(at)brave(dot)cs(dot)uml(dot)edu>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: Calculating Percentages
Date: 2003-05-14 15:24:35
Message-ID: 200305140824.35328.josh@agliodbs.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Richard Huxton 2003-05-14 15:28:06 Re: Type coercion on column in a query
Previous Message Tom Lane 2003-05-14 15:23:39 Re: Calculating Percentages