I have a table named tc_fuel
that is receiving all the fuel related data from GPS vehicles, and I want to get the last "Tank Usage" to calculate MPG for a whole tank, but the readings when the tank is full (100) some times repeat after 2 or 3 or more rows, so I am left with 2 values of a 100 next to each other, I want to be able to get the last "fillup" starting and ending ids.
What I have so far:
SELECT
"tc_fuel".deviceid,
"tc_fuel"."id",
"tc_fuel".fuel,
"tc_fuel"."fuelUsed",
"tc_fuel"."fuelUsed"
FROM "tc_fuel"
WHERE fuel=100
AND deviceid=19
ORDER BY ID
DESC LIMIT 2
Then I go into PHP to check if the id difference its over 100 records to check that the fuel values are not next to each other, but this is doing some more work than it should I was wondering if is there a better way.
For example this vehicle started on a full tank and then drop to 6% tank and did a full tank fill, I want to be able to grab all the data of the last tank.
id | deviceId | fuel
------+-----------+-------
1 | 19 | 100 <-- This should be starting point
2 | 19 | 97
3 | 19 | 100
4 | 19 | 96
5 | 19 | 94
6 | 19 | .... (keeps dropping)
7 | 19 | 33
8 | 19 | 31
9 | 19 | 30
10 | 19 | ....
11 | 19 | 6
12 | 19 | 5
13 | 19 | 6 <-- This should be end point (will flag this id as processed)
14 | 19 | 100 <-- Starts all over again in next iteration of the php script
15 | 19 | 99
16 | 19 | 98
17 | 19 | 100
18 | 19 | 99
19 | 19 | 97
20 | 19 | 96
21 | 19 | ....