Re: Generating percentages of numbers

From: Date: Thu, 22 Feb 2001 11:07:24 +0000
Subject: Re: Generating percentages of numbers
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-41127@lists.php.net to get a copy of this message
> Better (if it works): > SELECT (hitcount / MAX(hitcount)) * 100 AS percentage FROM sometable Nope unfortunately that does not work :( Firstly using MAX() will not work as that finds the largest value in the group; SUM() would be more appropriate as it finds the total for that column in the group. But you can't group the data here (you want to select all page rows from the table) so these grouping functions are not usable. He will need to do two queries: <?php $result = mysql_query("SELECT SUM(hitcount) FROM pages"); list($totalhits) = mysql_fetch_row($result); $result = mysql_query("SELECT (hitcount / $totalhits) * 100 AS percentage FROM pages"); ?> I think... From: "Christian Reiniger" <creinig@mayn.de> > On Thursday 22 February 2001 00:04, Simon Garner wrote: > > > I have a database of numbers: > > > > TOTAL HITS: 1000 > > PAGE 1: 500 > > PAGE 2: 250 > > PAGE 3: 250 > > > > How can I query these numbers and display back a percentage number? > > For instance with the numbers above: > > > > PAGE 1: 50% > > PAGE 2: 25% > > PAGE 3: 25% > > > > Or is their a process by which to store numbers for easily getting > > this to work? > > SELECT (hitcount / 1000) * 100 AS percentage FROM sometable Cheers Simon Garner

« previous php.general (#41127) next »