
Originally Posted by
r937
Code:
SELECT latest.gps_unit_id
, latest.odometer - earliest.odometer AS diff_odometer
, latest.total_fuel - earliest.total_fuel AS diff_total_fuel
, latest.engine_hours - earliest.engine_hours AS diff_engine_hours
, TIMEDIFF(latest.created - earliest.created) AS diff_created
FROM ( SELECT gps_unit_id
, MIN(created) AS min_created
, MAX(created) AS max_created
FROM fms1
WHERE created BETWEEN '2012-03-26' AND NOW()
GROUP
BY gps_unit_id ) AS mm
INNER
JOIN fms1 AS earliest
ON earliest.gps_unit_id = mm.gps_unit_id
AND earliest.created = mm.min_created
INNER
JOIN fms1 AS latest
ON latest.gps_unit_id = mm.gps_unit_id
AND latest.created = mm.max_created
Hi, sorry for not getting back to you urlier. Moving house took a lott of time out of my sceduele.
It does looks elegant to me, but I don't think my mySQL skills are good enough to fully understand this one...
I'm not sure this does what I want it to do. I get results like this:
Code:
gps_unit_id diff_odometer diff_total_fuel diff_engine_hours diff_created
76 1667.94500000001 704 57.650390625 88:01:01
79 1147.35 503.5 45.6000061035156 76:17:04
79 1147.35 503.5 45.6000061035156 76:17:04
79 1147.35 503.5 45.6000061035156 76:17:04
79 1147.35 503.5 45.6000061035156 76:17:04
80 1432.22499999998 682 62.7998046875 88:27:27
81 1329.71500000003 603 44.1005859375 83:09:57
90 1258.73999999999 672 38.89990234375 82:46:55
91 1364.57000000001 688 41 83:07:57
92 1886.78 785 40 84:33:03
(hope that's readable.)
I changed the dates to 2012-04-02 and NOW to get the results for this week.
also timediff() works with two commaseperated values.
You should know, there are currently 7 units submitting data into the fms1 table; 76, 79, 80, 81, 90, 91 and 92
4 belong to one user, and 3 to another.
If I just run the inside select:
Code:
SELECT gps_unit_id, MIN( created ) AS min_created, MAX( created ) AS max_created
FROM fms1
WHERE created
BETWEEN '2012-04-02'
AND NOW( )
GROUP BY gps_unit_id
I get:
Code:
gps_unit_id min_created max_created
76 2012-04-02 04:14:19 2012-04-05 20:15:20
79 2012-04-02 14:02:40 2012-04-05 18:19:44
80 2012-04-02 04:34:55 2012-04-05 21:02:22
81 2012-04-02 05:07:14 2012-04-05 16:17:11
90 2012-04-02 06:57:33 2012-04-05 17:44:28
91 2012-04-02 05:53:56 2012-04-05 17:01:53
92 2012-04-02 04:33:26 2012-04-05 17:06:29
except that it's for all the units. In this I don't understand the group by function. If I leave it out, I just get one record:
Code:
gps_unit_id min_created max_created
76 2012-04-02 04:14:19 2012-04-05 21:02:22
note the difference in times for the min_created and max_created fields.
for unit 76 the dates should be: 2012-04-02 04:14:19 and 2012-04-05 20:15:20
found by sorting (SELECT *FROM `fms1`WHERE `created`BETWEEN '2012-04-02' AND NOW( ) AND `gps_unit_id` =76 ORDER BY `fms1`.`created` DESC LIMIT 0 , 30 ) ASC and DESC
We can select the units beloging to one user with a inner join
Code:
INNER JOIN gps_units ON gps_units.id = fms1.gps_unit_id
So, each user has units
each unit submits data to the fms1 table.
I like for each unit belonging to a user the difference between first and last record within a time period. I think you got that last part right, but somehow selecting or grouping the right units is not working as it should.
Please let me know what kind of info you need to fix this.
Many thanks in advance
Jeroen
Bookmarks