mysql - PHP producing incorrect arithmetic results -


i have php script launches mysql query , returns result set aggregated columns.

i need further aggregations on these column, , such have done in script.

the problem first 7 results out of 20 limit, calculations blatantly incorrect.

is fact table large? (3.5m rows)

can complete aggregations @ sql level? a aggregation , b aggregation, can column c = b / a?

output

//query         $resultsquery = $db->prepare("select 4_nationality.nationality, count(distinct(txn_id)) numtrans,                                         sum(sales) sales, sum(units) units, yrqtr                                         1_txns inner join 4_nationality using (nationality_id)                                         yrqtr :period                                         group nationality_id                                          order numtrans desc                                         limit 20");         $resultsquery->bindparam(":period", $period);         $resultsquery->execute();          //build table          echo "<div class='col-xs-12 col-sm-10'>";         echo $select;           echo "<table class='table table-striped'><tr>";             echo "<th>nationality</th>";             echo "<th># trans</th>";             echo "<th>sales</th>";             echo "<th>units</th>";             echo "<th>atv</th>";             echo "<th>upb</th>";             echo "<th>arp</th>";         echo "</tr>";          while($row2 = $resultsquery->fetch(pdo::fetch_assoc)){              //further calculations              $nat = $row2['nationality'];             $numtrans = number_format($row2['numtrans'], 0);             $sales = number_format($row2['sales'], 0);             $units = number_format($row2['units'], 0);             $atvc = $sales / $numtrans;             $atv = number_format($atvc, 2);             $upbc = $units / $numtrans;             $upb = number_format($upbc, 1);             $arpc = $sales / $units;             $arp = number_format($arpc, 2);                  //display                 echo "<tr>";                     echo "<td>".$nat."</td>";                     echo "<td>".$numtrans."</td>";                     echo "<td>".$sales."</td>";                     echo "<td>".$units."</td>";                     echo "<td>".$atv."</td>";                     echo "<td>".$upb."</td>";                     echo "<td>".$arp."</td>";                 echo "</tr>";          }          echo "</table>";         echo "</div>";   

edit: many both of answers, difficult choose accept both helped me in different ways. have decided accept answer offers more detail regarding question title.

this may because performing arithmetic on formatted strings.

try:

$nat = $row2['nationality']; $numtrans = number_format($row2['numtrans'], 0); $sales = number_format($row2['sales'], 0); $units = number_format($row2['units'], 0); $atvc = $row2['sales'] / $row2['numtrans']; $atv = number_format($atvc, 2); $upbc = $row2['units'] / $row2['numtrans']; $upb = number_format($upbc, 1); $arpc = $row2['sales'] / $row2['units']; $arp = number_format($arpc, 2); 

edit:

actually i'm sure whats happenning:

echo '15,263,316' / '568,393'; // 0.026408450704225 echo 15263316 / 568393;        //26.853455267746 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -