Re: [PHP] Feeling a bit brain dead, please help in maths: averages

This is a discussion on Re: [PHP] Feeling a bit brain dead, please help in maths: averages within the PHP General forums, part of the PHP Programming Forums category; Hey, Thanks for replying. Actually I made that example simple, i actually need the average of 7 to 9 fields.......


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-17-2003
Ryan A
 
Posts: n/a
Default Re: [PHP] Feeling a bit brain dead, please help in maths: averages

Hey,
Thanks for replying.

Actually I made that example simple, i actually need the average of 7 to 9
fields....and i dont want to run 7 selects :-(
I know its been done on elance....but how?

Thanks,
-Ryan


> off the top of my head:
>
> SELECT AVG(age), SUM(salTotal)
> FROM blah
> WHERE section = 3
>
> i'm not sure if you can use the 2 functions in the same query,
> you might have to break it into 2 queries.
>
> Craig
>
> -----Original Message-----
> From: Ryan A [mailto:ryan@coinpass.com]
> Sent: Wednesday, September 17, 2003 3:08 PM
> To: php-general@lists.php.net
> Subject: [php] Feeling a bit brain dead, please help in maths: averages
>
>
> Hey,
> Been working a bit too much i guess so am feeling braindead...
>
> I have a couple of records in the database, i am doing a: "select
> age,salTotal from blah where section=3"
> this is getting me a list of records, how do i calculate the average age

and
> salary total (salTotal) to display in my php page?
>
> Cheers,
> -Ryan A
>
>
>
>
>
>
>
> We will slaughter you all! - The Iraqi (Dis)information ministers site
> http://MrSahaf.com
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
>

Reply With Quote
  #2 (permalink)  
Old 09-17-2003
Craig Lonsbury
 
Posts: n/a
Default RE: [PHP] Feeling a bit brain dead, please help in maths: averages

> -----Original Message-----
> From: Ryan A [mailto:ryan@coinpass.com]
> Sent: Wednesday, September 17, 2003 3:21 PM
> To: craig@topdraw.com; php-general@lists.php.net
> Subject: Re: [php] Feeling a bit brain dead, please help in maths:
> averages
>
>
> Hey,
> Thanks for replying.
>
> Actually I made that example simple, i actually need the average
> of 7 to 9
> fields....and i dont want to run 7 selects :-(
> I know its been done on elance....but how?
>
> Thanks,
> -Ryan


this should do the trick... but it's not tested ;)

#assuming you are using mysql and your query
#has successfully executed and your results
#are in $resultSet

$rowCount = 0;
$ageSum = 0;
$salarySum = 0; #use as many sums as needed

while ($row = mysql_fetch_object($resultSet)) {
$ageSum += $row->age;
$salarySum += $row->sal;
$rowCount++;
}
$salaryAvg = $ageSum / $rowCount;

>
>
> > off the top of my head:
> >
> > SELECT AVG(age), SUM(salTotal)
> > FROM blah
> > WHERE section = 3
> >
> > i'm not sure if you can use the 2 functions in the same query,
> > you might have to break it into 2 queries.
> >
> > Craig
> >
> > -----Original Message-----
> > From: Ryan A [mailto:ryan@coinpass.com]
> > Sent: Wednesday, September 17, 2003 3:08 PM
> > To: php-general@lists.php.net
> > Subject: [php] Feeling a bit brain dead, please help in maths: averages
> >
> >
> > Hey,
> > Been working a bit too much i guess so am feeling braindead...
> >
> > I have a couple of records in the database, i am doing a: "select
> > age,salTotal from blah where section=3"
> > this is getting me a list of records, how do i calculate the average age

> and
> > salary total (salTotal) to display in my php page?
> >
> > Cheers,
> > -Ryan A
> >
> >
> >
> >
> >
> >
> >
> > We will slaughter you all! - The Iraqi (Dis)information ministers site
> > http://MrSahaf.com
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> > --
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> >

>
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:36 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0