有没有更好的方法来处理这种情况?
我有一个表格,每 5 分钟的“时间”块有几行“速度”。我想取这 5 分钟块中的 12 个块的平均值,以获得每小时的速度。我想出的虚拟方法是计算每个 5 分钟块的总和,然后将它们除以 12。
有没有更好的方法,无论是在编码简单性还是 sql 效率方面?
SELECT (
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
10 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
5 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
15 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
10 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
20 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
15 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
25 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
20 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
30 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
25 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
35 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
30 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
40 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
35 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
45 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
40 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
50 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
45 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
55 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
50 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
60 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
55 MINUTE) THEN speed ELSE 0 END) +
SUM(CASE WHEN time > DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
65 MINUTE) AND time < DATE_SUB(CURRENT_TIMESTAMP, INTERVAL
60 MINUTE) THEN speed ELSE 0 END)
)/12
FROM table
谢谢!