I have intraday stock data stored in a MySQL table that looks like this:
+----------+-------+ | tick | quote | +----------+-------+ | 08:00:10 | 5778 | | 08:00:11 | 5776 | | 08:00:12 | 5778 | | 08:00:13 | 5778 | | 08:00:14 | NULL | | 08:00:15 | NULL | | 08:00:16 | 5779 | | 08:00:17 | 5778 | | 08:00:18 | 5780 | | 08:00:19 | NULL | | 08:00:20 | 5781 | | 08:00:21 | 5779 | | 08:00:22 | 5779 | | 08:00:23 | 5779 | | 08:00:24 | 5778 | | 08:00:25 | 5779 | | 08:00:26 | 5777 | | 08:00:27 | NULL | | 08:00:28 | NULL | | 08:00:29 | 5776 | +----------+-------+
As you can see, there are some moments when data is not available (quote NULL ). What I would like to do is simply interpolate the steps. This means that each NULL value must be updated with the last available value. The only way I was able to do this is with cursors, which are rather slow due to the large amount of data. I'm basically looking for something like this:
UPDATE table AS t1 SET quote = (SELECT quote FROM table AS t2 WHERE t2.tick < t1.tick AND t2.quote IS NOT NULL ORDER BY t2.tick DESC LIMIT 1) WHERE quote IS NULL
Of course, this request will not work, but it should look like this.
I would appreciate any ideas on how this can be solved without cursors and temporary tables.
source share