Mysql实际和运行平均温度查询

时间:2016-12-17 11:07:09

标签: mysql d3.js

我的太阳能热水套的温度大约每3分钟记录一次,名为boiler

id - date - time - lowT - higT

e.g。

1 - 2016-12-06 - 00:41:26 - 37.69 - 23.38

由于互联网连接不良,电源故障或其他问题,可能会出现数据缺口。

为了在d3中进行演示,我需要流畅的选择:

time - lowTtd - higTyd - higTtd - higTyd - lowTav - higTav

where

time is the table.time in 3 minute steps e.g. 00:03:00, 00:06:00 ....

lowTtd is the table.lowT from today, but empty if Now() < time

lowTyd is the table.lowT from yesterday, but empty if Now() > time

自录制开始以来,lowTav正在运行avg + -30分钟的table.lowT

高...分别与低......相同...

为了达到这个目的,我有另一个带有时间步骤的表叫做intervaller:

start

00:00:00
00:03:00
00:06:00
.
.
23:57:00

目前我使用以下代码:

SELECT xx.start 
AS date, xx.lowTtd, xx.lowTyd, xx.higTtd, xx.higTyd, yy.lowTav, yy.higTav 
FROM (SELECT i.start,
time AS date, (CASE WHEN time<DATE_ADD(NOW(), 
INTERVAL -1 HOUR) THEN lowT ELSE '' END) AS lowTtd, (CASE WHEN time>DATE_ADD(NOW(), 
INTERVAL -1 HOUR) THEN lowT ELSE '' END) AS lowTyd, (CASE WHEN time<DATE_ADD(NOW(), 
INTERVAL -1 HOUR) THEN higT ELSE '' END) AS higTtd, (CASE WHEN time>DATE_ADD(NOW(), 
INTERVAL -1 HOUR) THEN higT ELSE '' END) AS higTyd 
FROM boiler, intervaler i 
WHERE HOUR(TIMEDIFF(DATE_ADD(NOW(), INTERVAL -1 HOUR), concat(date,' ',time)))<24 
AND time > i.start 
AND time < DATE_ADD(i.start, INTERVAL 3 MINUTE) 
GROUP BY i.start 
ORDER BY `time` ASC) xx, 
(SELECT i.start, AVG(b.lowT) AS lowTav, AVG(b.higT) AS higTav 
FROM intervaler i, `boiler` b 
WHERE b.time > DATE_ADD(i.start, INTERVAL -30 MINUTE) AND b.time < DATE_ADD(i.start, INTERVAL 30 MINUTE) 
GROUP BY i.start) yy WHERE xx.start = yy.start

但是,我确信有很大的改进空间,特别是在查询时间方面。 'boiler'包含少于5500个条目,但我的查询几乎需要1秒。

非常感谢如何改进我的代码。

谢谢你,并致以最诚挚的问候。

example of d3 output

0 个答案:

没有答案