Sam Grant
Sam Grant

Reputation: 442

mysql interpolate data

Is there an 'easy way' to grab two rows of data from a table, and add rows with values 'in-between'?

I want to grab a latitude, a longitude and a timestamp from each row. Compare the timestamp to the one from the previous row, and interpolate new rows if the timestamp is bigger than my minimum...grab two rows 1 minute apart and add rows for every 10 seconds...

Is using a stored procedure the best way to go about this? Easiest?

Currently using mySql and PHP...

Upvotes: 0

Views: 1439

Answers (1)

colinmarc
colinmarc

Reputation: 2471

I would just grab the data and do the math in PHP. SQL isn't all that versatile, and you'd be saving yourself a headache.

EDIT: Actually, just for the fun of it, you could make the math easier by left-joining to a calendar table.

First you need a table ints with the values 0-9. Then you can do something like:

SELECT cal.t, lat, lng FROM (
    SELECT {start_time} + INTERVAL (t.i*1000 + u.i*100 + v.i*10) SECOND as t
    FROM ints as t
    JOIN ints as u
    JOIN ints as v
    WHERE t <= {end_time}
) LEFT JOIN locations ON (cal.t = locations.stamp)

This would return a table with NULL values for lat and lng where there isn't an entry on the 10 second mark, so you could iterate through and do the math for just those. Keep in mind, this only works if you all the datapoints you do have (other than the start and end) land right on a 10-second mark.

Upvotes: 2

Related Questions