SQLite填充空值

时间:2016-07-11 17:13:14

标签: sql sqlite

所以我有一些带有NULL值的表,我希望用最后一个有效值填充。我可以选择一个带有更正列的新表,但我不确定如何更新数据库上的值,有人可以帮我更新语句吗?

select b.timeStamp, 
       b.var_5, 
     (select a.var_5  from wme_test as a where a.timeStamp<=b.timeStamp  and a.var_5 <> 'NULL' order by timestamp desc limit 1 )  as correctedVar5 
from wme_test as b

此查询的输出是

enter image description here

1 个答案:

答案 0 :(得分:2)

您可以使用相关更新来执行此操作。

update wme_test 
set var5 = (select var5 
            from wme_test 
            where tm >= wme_test.tm and var5 is not null 
            order by tm limit 1)
where var5 is null

Sample Fiddle

请注意,表中最早的时间戳无法使用此更新。如果之前为null,它仍为null