I’ve a table in which every x seconds a position is being written to. The table consists of the following columns:
lat | lon | time | .....
The ultimate goal is to make the track visible on a map with on the points where the device halted displaying a image. On the points where there are more than one entries over time the rows should be added together to display a row with a ‘timeSpent’ at the location which is the difference of the different rows at the same location. So:
lat | lon | timeFirst | timeSpent
For instance this is the ‘raw’ table:
x1,y1, 13:00:12 // Point 1
x1,y2, 13:01:34 // Point 2
x3,y3, 13:02:01 // Point 3
x3,y3, 13:03:03 // Point 3
x3,y3, 13:04:34 // Point 3
x3,y3, 13:05:12 // Point 3
x4,y6, 13:06:23 // Point 4
On point 3 there are multiple entries. Which should be joined together to the following table:
x1,y1, 13:00:12, null
x1,y2, 13:01:34, null
x3,y3, 13:02:01, 13:05:12
x4,y6, 13:06:23, null
I would really love to have a native MySQL query which makes the result table. But if it’s necessary I could use PHP to parse the source table.
Can anyone help me to get the wanted table?
The problem that it is not clear in your example is that if you add another row to it with values:
It will still be
Point 3, but you’ll be expecting it to be considered as another point in the sequence. In order to do so you should have a query that would prepare the data like this (noticex3,y3values):Once you have this, you can easily group by. The query that would help you do this is the following:
So, all you need to do with that query is group it by and get the min and max values (and null them if they’re equal). So in order to get the result you’re looking for you should run the following query:
NOTE: This will also return the counter column. You can get rid of it by wrapping the previous query into another select statement such as:
But that will decrease considerably performance. So, my advice would be just not to use that column in your application.
Result with the counter column removed: