Current location: Hot Scripts Forums » Programming Languages » PHP » SUM and how to get the total value?


SUM and how to get the total value?

Reply
  #1 (permalink)  
Old 06-28-05, 10:48 AM
Programme Programme is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
SUM and how to get the total value?

My goal is to get the total value from all the records in my DB. A records has the field 'value' and the field 'qty' (quantity).
So far i have this working script:

PHP Code:

$select "SELECT * FROM table";

$data mysql_db_query('database'$select) or die("DIE");

while(
$row mysql_fetch_array($data))
   {
      
$uid $row['uid'];

      
$totals mysql_query("SELECT (SUM(value) * qty) AS totalSum FROM table WHERE uid = '"$uid ."'") or die("DIE");

      print(
"$total<br />");
   } 
This gives me the total result for each row on a line. If i had a Two record with the value and quantity like this: 2.00 | 3 & 1.50 | 2

The result i would get is: 6.00 and 3.00. The result i want is 9.00. I want to get the total result of all my records. How can this be done? Thanks
Reply With Quote
  #2 (permalink)  
Old 06-28-05, 01:36 PM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
remove the WHERE clause and you will get the sum of all records.
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #3 (permalink)  
Old 06-28-05, 02:23 PM
Programme Programme is offline
Newbie Coder
 
Join Date: Dec 2003
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Sorry

Sorry, removing the WHERE clause will SUM all the fields yes, but it will SUM the fields and then duplicate it by the first qty value it finds, in this case 3. So with the values 2.00 | 3 and 1.50 | 2, it will add 1.50 to 2.00 witch is 3.50 and duplicate by 3 witch is 7. The value i need is 9.00.

I need the script to get the total value for a row and then add all the rows together to get a single total value. The WHERE clause is there to get the value for that row as it is inside the { }.
Reply With Quote
  #4 (permalink)  
Old 06-30-05, 03:17 PM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Although I do not clearly understand what are you trying to achieve, but in your case you do not need the SUM aggregator at all since you loop thru every record you are SUMming. Simply:

$select = "SELECT * FROM table";
$data = mysql_db_query('database', $select) or die("DIE");
$sum = 0;

while($row = mysql_fetch_array($data))
{
$uid = $row['uid'];

$q = mysql_query("select value, qty from table where uid=$uid");
$r = mysql_fetch_assoc($q);
$sum += $r['value'] * $r['qty'];
}

echo $sum;
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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

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

Forum Jump


All times are GMT -5. The time now is 06:51 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.