pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Josh Berkus <josh@agliodbs.com>
To: David Siegal <dsiegal@brave.cs.uml.edu>
Cc: pgsql-sql@postgresql.org
Subject: Re: Calculating Percentages
Date: Wed, 14 May 2003 08:24:35 -0700
Message-ID: <200305140824.35328.josh@agliodbs.com> (raw)
In-Reply-To: <Pine.LNX.4.44.0305081102100.30051-100000@brave.cs.uml.edu>
References: <Pine.LNX.4.44.0305081102100.30051-100000@brave.cs.uml.edu>

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



view thread (13+ messages)  latest in thread

Message-ID: <200305140824.35328.josh@agliodbs.com>
Permalink:  ../200305140824.35328.josh@agliodbs.com/
Also on:    postgresql.org/message-id/200305140824.35328.josh@agliodbs.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: josh@agliodbs.com, dsiegal@brave.cs.uml.edu
  Subject: Re: Calculating Percentages
  In-Reply-To: <200305140824.35328.josh@agliodbs.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox