I have a database table fuel which has a fID field (primary, autoincrement), vID (for vehicle ID), volume (for amount of fuel in litres), price (for the cost of fuel per litre), meter (for the current odometer of the vehicle on the date of filling), date (the date the vehicle is being filled up), vendorID (for the fuel supplier or just where the vehicle was filled up), notes (any additional notes on the vehicle or fuel) and type (the fuel grade).
I want to make a query that would calculate the cost of fuel per each fill by multiplying the volume (litres bought) by price (cost per litre) then adding the total for all fuel entries for a particular vehicle, then lastly adding the cost of all the fuel entries in the table (the fID is unique but the vID gets repeated (as a vehicle will be filled up now and again).
This is what I'm running now but it only returns the cost of each fill up (not adding up the other fill ups).
Select
date_format(f1.date, '%y-%m-%y %H:%i:%s) as date,
f1.meter as mileage,
case when f2.meter is null then 0
else f1.meter - f2.meter
end as distance
f1.fID,
f1.volume,
f1.volume * f1.price as cost
from
fuel f1
left outer join
fuel f2
On
f2.date = (
select
max(date)
from
fuel
where
fuel.vID = f1.vID
and
fuel.date < f1.date
)
where
f1.ID = ?
Order by f1.date
Any help will be highly appreciated.