MYSQL QUERY 用平均值替换一行中的 NULL 值

标签 mysql

我正在使用 mysql 数据库存储大量卫星数据,这些数据集有很多数据缺口。 我想用该点附近 1 小时(或更短)的平均值替换 NULL 值。 到目前为止,我已经找到了如何用以前的已知值替换 NULL 值:

UPDATE mytable
SET number = (@n := COALESCE(number, @n))
ORDER BY date;

来自这篇文章:SQL QUERY replace NULL value in a row with a value from the previous known value

我的 table 看起来像

+---------------------+--------+
| date                | P_f    |
+---------------------+--------+
| 2001-01-01 20:20:00 |   1.88 | 
| 2001-01-01 20:25:00 |   NULL | 
| 2001-01-01 20:30:00 |   NULL | 
| 2001-01-01 20:35:00 |   1.71 | 
| 2001-01-01 20:40:00 |   NULL | 
| 2001-01-01 20:45:00 |   NULL | 
| 2001-01-01 20:50:00 |   NULL | 
| 2001-01-01 20:55:00 |  1.835 | 
| 2001-01-01 21:00:00 |  1.918 | 
| 2001-01-01 21:05:00 |  1.968 | 
| 2001-01-01 21:10:00 |  2.004 | 
| 2001-01-01 21:15:00 |  1.924 | 
| 2001-01-01 21:20:00 | 1.8625 | 
| 2001-01-01 21:25:00 |   1.94 | 
| 2001-01-01 21:30:00 | 2.0375 | 
| 2001-01-01 21:35:00 |  1.912 | 

我想用该日期时间附近的平均值替换 NULL 值。 例如我想替换 ,

| 2001-01-01 20:50:00 |   NULL |

平均在

select AVG(P_f) from table where date between '2001-01-01 20:30' and '2001-01-01 21:10';

保罗

最佳答案

我承认这不是最优雅的,但它应该能满足您的需求。

我不确定您要如何处理那些小时平均值为 NULL 的 NULL 值。在下面的示例中,这些将更新为 -1。

create table myTable
(myDate datetime not null,
P_f decimal(10,5) default null
);

insert into myTable(myDate,P_f) values ('2001-01-01 20:20:00',1.88);
insert into myTable(myDate,P_f) values ('2001-01-01 20:25:00',NULL);
insert into myTable(myDate,P_f) values ('2001-01-01 20:30:00',NULL);
insert into myTable(myDate,P_f) values ('2001-01-01 20:35:00',1.71);
insert into myTable(myDate,P_f) values ('2001-01-01 20:40:00',NULL);
insert into myTable(myDate,P_f) values ('2001-01-01 20:45:00',NULL);
insert into myTable(myDate,P_f) values ('2001-01-01 20:50:00',NULL);
insert into myTable(myDate,P_f) values ('2001-01-01 20:55:00',1.835);
insert into myTable(myDate,P_f) values ('2001-01-01 21:00:00',1.918);
insert into myTable(myDate,P_f) values ('2001-01-01 21:05:00',1.968);
insert into myTable(myDate,P_f) values ('2001-01-01 21:10:00',2.004);
insert into myTable(myDate,P_f) values ('2001-01-01 21:15:00',1.924);
insert into myTable(myDate,P_f) values ('2001-01-01 21:20:00',1.8625);
insert into myTable(myDate,P_f) values ('2001-01-01 21:25:00',1.94);
insert into myTable(myDate,P_f) values ('2001-01-01 21:30:00',2.0375);
insert into myTable(myDate,P_f) values ('2001-01-01 21:35:00',1.912);
insert into myTable(myDate,P_f) values ('2001-01-02 20:40:00',NULL);

-- Insert copy of null value P_f rows into myTable with 1 hour average about myDate 
insert into myTable
(myDate,P_f)
select t.myDate,ifnull((select avg(P_f) from myTable t1 where t1.myDate between t.myDate - interval 1 hour and t.myDate +interval 1 hour),-1) as hourAvg
from myTable t
where t.P_f is null;

-- delete rows where P_f is null
delete from myTable
where P_f is null;

关于MYSQL QUERY 用平均值替换一行中的 NULL 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8816463/

相关文章:

mysql - MySQL Spatial 中的质心投影

mysql - 使用where条件将数据从一个表插入到另一个表中

mysql - 如何使用 rails 在 mysql 数据库中查找浮点值

mysql - solr 增量导入 "fetches"但不导入 "process"

mysql - Jaro-winkler 函数 : why is the same score matching very similar and very different words?

mysql - 简单的SQL查询问题

mysql - 从日期中获取年份和月份

java - 为什么要使用JSTL?当我们在JSP文件中使用带有连接字符串的数据库连接来获取servlet发送的数据时会有什么危害

php - Laravel fromTable 不适用于模型 updateOrCreate 函数

mysql - 从数据库中的信息创 build 计精美的简历