0
Hello, I started a query from a mysql database where the distance of each commute of a driver is saved along with the time:
DATE_FORMAT(l.dateGenerated,'%d/%m/%Y %H:%i') AS Day ,
d.name AS Conductor,
ROUND((l.odometer/1000),3) AS Route
from location l
left join device d on (d.id = l.deviceId)
| Day | Conductor | Routes |
|------------------|-----------|--------|
| 05/07/2019 19:12 | PAULO | 0 |
| 05/07/2019 19:14 | MARCOS | 0 |
| 05/07/2019 19:14 | MARCOS | 0,488 |
| 05/07/2019 19:16 | MARCOS | 0,651 |
| 05/07/2019 19:16 | PAULO | 0,533 |
| 05/07/2019 19:16 | MARCOS | 0,703 |
| 05/07/2019 19:16 | PAULO | 0,571 |
| 05/07/2019 19:17 | MARCOS | 0,755 |
| 05/07/2019 19:17 | PAULO | 0,602 |
| 05/07/2019 19:20 | PAULO | 0,757 |
| 06/07/2019 08:10 | MARCOS | 0,807 |
| 06/07/2019 08:11 | MARCOS | 0,911 |
| 06/07/2019 08:12 | MARCOS | 1,015 |
| 06/07/2019 08:13 | MARCOS | 1,067 |
| 06/07/2019 08:15 | MARCOS | 1,223 |
| 06/07/2019 08:16 | PAULO | 0,791 |
| 06/07/2019 08:16 | PAULO | 0,833 |
| 06/07/2019 08:19 | PAULO | 1,01 |
| 06/07/2019 08:20 | PAULO | 1,063 |
| 06/07/2019 19:16 | PAULO | 1,114 |
| 06/07/2019 19:17 | PAULO | 1,165 |
But what I need exactly and am not able to calculate is the daily distance traveled by each driver, I imagined taking the last measurement of the odometer of each day and subtract by the first measurement, to generate something like this:
| Day | Conductor | Traveled Day |
|--------|-----------|--------------|
| 05/jul | Paulo | 0,757 |
| 05/jul | Marcos | 0,755 |
| 06/jul | Paulo | 0,408 |
| 06/jul | Marcos | 0,468 |
Some "Hero" could help me?
If you explain how you arrived at the results presented in Traveled Day maybe we can help you.,
– anonimo
Getting Traveled Day is my goal here, the above example is the desired that I could not bring.
– Acimar Rocha
Just add up all the routes of each day to each conductor? I imagine you can explain the calculation that you expect to be done to achieve the desired result, otherwise it will be impossible to guess and try to help you.
– anonimo
@anonimo actually is not just add up. You need to add up the day and discount the day before
– Sorack
What version of
MySQL
?– Sorack
Your solution looks ideal @Sorack! Unfortunately I am running version 5.7.26-0 Ubuntu 0.19.04.1
– Acimar Rocha
@Acimarrocha indicated in my reply also the query referring to previous versions
– Sorack