I got a table which tracks positions over time. This table contains a lot of rows and cannot be changed in format.
Every position is inserted with an id, the actual position and a timestamp (datetime). The position itself is inserted in a concatenated string which can be converted to x/y coordinates using custom functions.
I want to create a procedure, and lists all known positions and their appropiate timestamps for a certain. No problem so far. But also want to create an output column which indicates the difference in position and time.
To clarify. What I have:
Id | Timestamp | Foreign_Id | POS
----------------------------------------------------------
1 | 2011-02-20 00:00:00 | 2 | PositionAsString
----------------------------------------------------------
2 | 2011-02-20 00:00:05 | 2 | PositionAsString
----------------------------------------------------------
3 | 2011-02-20 00:00:15 | 2 | PositionAsString
----------------------------------------------------------
4 | 2011-02-20 00:00:37 | 2 | PositionAsString
The Coordinates of the Position are available via the functions getX and getY which return float values for X and Y.
The difference in position would be calculated using the Pythagorean theorem as it is only 2D.
What I want
Id | Timestamp | Foreign_Id | POS | DiffPos | Speed
---------------------------------------------------------------------------
1 | 2011-02-20 00:00:00 | 2 | PositionAsString | 0 | 0
---------------------------------------------------------------------------
2 | 2011-02-20 00:00:05 | 2 | PositionAsString | 20 | 4
---------------------------------------------------------------------------
3 | 2011-02-20 00:00:10 | 2 | PositionAsString | 10 | 2
---------------------------------------------------------------------------
4 | 2011-02-20 00:00:20 | 2 | PositionAsString | 10 | 1
So now my issue is how to calculate the difference to the previous row.
Important is, that the result set is narrowed down to the foreign key befor any unefficient calculations are performed because the table has many rows.
Thanks for the suggestion, however as this is complicated and inefficient I am now using another solution. As I stated above I am unable to change the format of the source table. But what is possible was to modify the trigger which was writing to this tracking table so that it did compute the differences before inserting the new row.
This will of course not work for existing entries but this is acceptable.