Jump to content

SUM function is not working


tobitimac

Recommended Posts

Hi

 

Can someone help me on this.. The SUM(quantityHand) is not summing up the quantityHand

 

if(isset($select)&&$select!=""){

   // Get records from database (table "name_list").
//$result=@mysql_query("select * FROM `inventory` WHERE id='$select' GROUP BY `itemNumber` ORDER BY `itemNumber`");
$result=@mysql_query("SELECT DISTINCT itemNumber, itemDesc, quantityHand, SUM(quantityHand) AS quantityHand FROM inventory  WHERE id='$select' GROUP BY `itemNumber` ORDER BY `itemNumber`");
while ($row=@mysql_fetch_assoc($result)) {

    echo"<b>";
echo "<tr><td style=\"font-size: 15px;\"><b>";
echo $row['itemNumber'];
echo "</td><td style=\"font-size: 15px;\"><b>";
echo $row['itemDesc'];
//echo "</td><td style=\"text-align: right;\"><b>";
echo "</td><td style=\"font-size: 15px; text-align: center;\"><b>";
echo $row['quantityHand'];
    echo"</b>";

// End While loop
}
// End if statement.
}

Link to comment
Share on other sites

For one thing, you have the same field name used twice. Once as the actual field name, and once as the name for the SUM. Change your query to something like this and see what it looks like:

$result=@mysql_query("SELECT DISTINCT itemNumber, itemDesc, quantityHand, SUM(quantityHand) AS quantityHandSUM FROM inventory  WHERE id='$select' GROUP BY `itemNumber` ORDER BY `itemNumber`");

 

 

 

 

Link to comment
Share on other sites

Don't SELECT DISTINCT, just SELECT. I don't have a mysql server to test on right now, but I believe if you use SELECT DISTINCT it only selects each DISTINCT itemNumber. To SUM them and group you need to select duplicate item numbers.

Link to comment
Share on other sites

When you changed the alias name in the query, did you change your php code to use that new name?

 

What does show up on the web page where the quantity should be? Do you get a zero or is it blank? Have you done a 'view source' in your browser, in case the output is present but is not being rendered?

 

Are you sure you have any rows that match WHERE id='$select', because the SUM() will return a null value if there are no rows. Try using var_dump($row['quantityHandSUM']) to see what you are actually getting.

 

It would help if you echoed $select to make sure it has the value you expect (just because it is set and not an empty string, doesn't mean it has the value you expect) and it would help if you showed us the rows from your table that the query should be operating on.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.