我有一个MySQL数据库,测量的温度来自三个不同的传感器。每次更新传感器时,新温度都会写入数据库。因此每个值都有不同的时间戳。
我想计算每个时间戳的三个房间的平均温度。有没有SQL-Query可以做到这一点?
Table A: timestamp + value
0000-00-00 00:00:00.000 19.6
2018-01-22 20:45:31.594 19.6
2018-01-22 20:48:20.344 19.7
2018-01-22 20:50:54.567 19.8
2018-01-22 20:53:14.594 19.9
2018-01-22 20:55:20.087 20.0
Table B: timestamp + value
0000-00-00 00:00:00.000 18.4
2018-01-22 20:50:53.700 18.4
2018-01-22 20:53:38.417 18.5
2018-01-22 20:56:08.668 18.6
2018-01-22 20:58:24.422 18.7
2018-01-22 21:00:25.707 18.8
Table C: timestamp + value
0000-00-00 00:00:00.000 20.1
2018-01-22 20:47:07.669 20.1
2018-01-22 20:49:32.196 20.2
2018-01-22 20:49:42.184 20.2
2018-01-22 20:51:42.177 20.2
Result:
2018-01-22 20:45:31.594 (19.6+18.4+20.1)/3
2018-01-22 20:47:07.669 (19.6+18.4+20.1)/3
2018-01-22 20:48:20.344 (19.7+18.4+20.1)/3
2018-01-22 20:49:32.196 (19.7+18.4+20.2(/3
…
答案 0 :(得分:0)
使用时间戳加入所有三个表,然后查找然后计算温度的平均值。
从T1.TIMESTAMP = T2.TIMESTAMP JOIN T3 ON T1.TIMESTAMP = T3.TIMESTAMP >
答案 1 :(得分:0)
如果要在系统中添加传感器,则可能需要为它们创建单个统一表。但这适用于三个传感器:
select timestamp, avg(value) as average_value from (
select timestamp, value from table_a
union all
select timestamp, value from table_b
union all
select timestamp, value from table_c
)
group by timestamp
答案 2 :(得分:0)
假设我们有表C和某个时间戳' 2018-01-22 20:51:42.177',我想在此之前使用表A和表B中的最后一个时间戳建立平均值表C中的时间戳如下:表B:' 2018-01-22 20:50:53.700'和表A:' 2018-01-22 20:50:54.567'反之亦然其他表格
这个怎么样:
select
c.timestamp,
(
c.value
+ (select coalesce(min(b.value),0.0) from b where b.timestamp =
(select max(b2.timestamp) from b as b2
where b2.timestamp <= c.timestamp))
+ (select coalesce(min(a.value),0.0) from a where a.timestamp =
(select max(a2.timestamp) from a as a2
where a2.timestamp <= c.timestamp))
) / 3 as average
from c