To calculate the MPG values from a Fuel
table I'm using this SQL Server query which works great:
SELECT
CASE
WHEN Fuel.Quantity = 0 THEN 0
ELSE (Fuel.Odometer - ISNULL((SELECT TOP 1 Dupe.Odometer FROM Fuel AS Dupe WHERE Dupe.MainID = Fuel.MainID AND Dupe.FuelingDateTime < Fuel.FuelingDateTime ORDER BY Dupe.FuelingDateTime DESC, Dupe.FuelID),0)) / Fuel.Quantity
END AS MPG
FROM Fuel
WHERE MainID = 1
When I translate it to SQLite query I get an error:
Near "1" : syntax error
The SQLite query is:
SELECT CASE
WHEN Fuel.Quantity = 0 THEN 0
ELSE
(Fuel.Odometer - IFNULL((Select TOP 1 Dupe.Odometer FROM Fuel As Dupe WHERE Dupe.MainID = Fuel.MainID AND Dupe.FuelingDateTime < Fuel.FuelingDateTime ORDER BY Dupe.FuelingDateTime DESC, Dupe.FuelID),0)) / Fuel.Quantity
END As MPG
FROM Fuel
WHERE MainID = 1
Is it the temporary table ('Dupe')? How do I write the query in SQLite?