I have a script that calculates the aveage fuel consumption the last 30 days, but some users are reporting errors in the calculation.

This is what I've got:

//Calculate the total km for the last 30 days
    $result = mysqli_query($con,"SELECT MIN(km) AS minikm, MAX(km) AS maxkm FROM diesel WHERE diesel.dato >= '$dx' AND userid = ".$_COOKIE['userid']." ORDER BY diesel.dato DESC");
    while($row = mysqli_fetch_array($result)){ $mileage = $row['maxkm'] - $row['minikm'];}

//Sum up all the Liters of diesel
    $result = mysqli_query($con,"SELECT SUM(liter) AS totalfuel FROM diesel WHERE diesel.dato >= '$dx' AND userid = ".$_COOKIE['userid']." ORDER BY diesel.dato DESC");
    while($row = mysqli_fetch_array($result)){ $totalfuel = $row['totalfuel'];} 

$averagefuel = $totalfuel / $mileage * 10;

When troubleshooting this I noticed I should not include the liters in the first record in the calculation. Only the total kms. So How can I do that ?

有帮助吗?

解决方案

If I had to speculate on what the users are thinking, the problem is that the fuel inputs over a month are somewhat unrelated to the fuel consumption during the month. In other words, the tank may start empty full or somewhere in-between. And, it might end empty, full, or somewhere in-between.

That means that your calculation can be off by up to a tank-full of fuel on either side.

Now, this could be mitigated by particular circumstances. For instance, perhaps the vehicles are being driven every day, with their fuel tanks filled at the end of the day. So, at midnight, you have a reasonable assumption that the tanks are full. I would be surprised if this were the case, because the inventory of fuel in the cars costs money. They could hire me as a consultant to improve their balance sheet ;) (just for the record, that is not my specialty).

As a small note, you can do the full calculations in SQL. You don't need to use two queries or any client side calculations:

SELECT 10 * SUM(liter) / (MAX(km) - MIN(km)) as AvgFuel
FROM diesel
WHERE diesel.dato >= '$dx' AND userid = ".$_COOKIE['userid'].";

其他提示

I think you need all the time add $mileage. Try this:

//Calculate the total km for the last 30 days
$mileage = 0;
    $result = mysqli_query($con,"SELECT MIN(km) AS minikm, MAX(km) AS maxkm FROM diesel WHERE diesel.dato >= '$dx' AND userid = ".$_COOKIE['userid']." ORDER BY diesel.dato DESC");
    while($row = mysqli_fetch_array($result)){ $mileage += $row['maxkm'] - $row['minikm'];}

//Sum up all the Liters of diesel
    $result = mysqli_query($con,"SELECT SUM(liter) AS totalfuel FROM diesel WHERE diesel.dato >= '$dx' AND userid = ".$_COOKIE['userid']." ORDER BY diesel.dato DESC");
    while($row = mysqli_fetch_array($result)){ $totalfuel = $row['totalfuel'];} 

$averagefuel = $totalfuel / $mileage * 10;

Gordon Linoff provide to you good solution. Try to use this:

//Calculate the total km for the last 30 days
$mileage = 0;
$result = mysqli_query($con,
"SELECT 10 * SUM(liter) / (MAX(km) - MIN(km)) as AvgFuel
FROM diesel
WHERE diesel.dato >= '$dx' AND userid = ".$_COOKIE['userid']);
$averagefuel = $steps = 0; 
while($row = mysqli_fetch_array($result)){ 
    $averagefuel += $row['AvgFuel']; 
    $steps++; 
} 
$averagefuel = $averagefuel/$steps;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top