Interpolate missing values in a MySQL table

前端 未结 3 1508
夕颜
夕颜 2021-01-13 20:06

I have some intraday stock data saved into a MySQL table which looks like this:

+----------+-------+
| tick     | quote |
+----------+-------+
| 08:00:10 |           


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-13 20:47

    The main problem here is reference to main query in subquery t2.tick < t1.tick. Because of this you can't simply wrap the subquery in a another subquery.

    If this is one time query and there is not so many data, you can do something like that:

    UPDATE `table` AS t1
    SET quote = (SELECT quote FROM (SELECT quote, tick FROM `table` AS t2 WHERE t2.quote IS NOT NULL) as t3 WHERE t3.tick < t1.tick ORDER BY t3.tick DESC LIMIT 1)
    WHERE quote IS NULL
    

    But really, really don't use that as it will be probably to slow. On each null quote, this query selects all data from table table and then from results it gets desired row.

提交回复
热议问题