MariaDB 运行天气数据总计(降雨量)

问题描述 投票:0回答:1

我每隔一分钟从气象站接收降雨数据,例如:

+---------------------+----------+
| tid                 | regn1min |
+---------------------+----------+
| 2024-09-09 05:00:11 |     0.00 |
| 2024-09-09 05:01:10 |     0.00 |
| 2024-09-09 05:02:10 |     0.00 |
| 2024-09-09 05:03:10 |     0.00 |
| 2024-09-09 05:04:10 |     0.00 |
| 2024-09-09 05:05:11 |     0.00 |
| 2024-09-09 05:06:10 |     0.00 |
| 2024-09-09 05:07:10 |     0.00 |
| 2024-09-09 05:08:10 |     0.41 |
| 2024-09-09 05:09:10 |     0.00 |
| 2024-09-09 05:10:11 |     0.00 |
| 2024-09-09 05:11:10 |     0.00 |
| 2024-09-09 05:12:10 |     0.00 |
| 2024-09-09 05:13:10 |     0.00 |
| 2024-09-09 05:14:10 |     0.41 |
| 2024-09-09 05:15:10 |     0.00 |
| 2024-09-09 05:16:11 |     0.00 |
| 2024-09-09 05:17:10 |     0.00 |
| 2024-09-09 05:18:10 |     0.00 |
| 2024-09-09 05:19:10 |     0.00 |
| 2024-09-09 05:20:10 |     0.00 |
| 2024-09-09 05:21:10 |     0.41 |
| 2024-09-09 05:22:11 |     0.00 |
| 2024-09-09 05:23:10 |     0.00 |
| 2024-09-09 05:24:10 |     0.00 |
| 2024-09-09 05:25:10 |     0.41 |
| 2024-09-09 05:26:10 |     0.00 |
| 2024-09-09 05:27:10 |     0.00 |
| 2024-09-09 05:28:10 |     0.00 |
| 2024-09-09 05:29:10 |     0.41 |
| 2024-09-09 05:30:10 |     0.00 |
| 2024-09-09 05:31:10 |     0.00 |
| 2024-09-09 05:32:10 |     0.41 |
| 2024-09-09 05:33:10 |     0.00 |
| 2024-09-09 05:34:11 |     0.00 |
| 2024-09-09 05:35:10 |     0.41 |
| 2024-09-09 05:36:10 |     0.00 |
| 2024-09-09 05:37:10 |     0.41 |
| 2024-09-09 05:38:10 |     0.00 |
| 2024-09-09 05:39:11 |     0.00 |
| 2024-09-09 05:40:10 |     0.00 |
| 2024-09-09 05:41:10 |     0.00 |
| 2024-09-09 05:42:10 |     0.00 |
| 2024-09-09 05:43:10 |     0.41 |
| 2024-09-09 05:44:10 |     0.00 |
| 2024-09-09 05:45:11 |     0.00 |
| 2024-09-09 05:46:10 |     0.00 |
| 2024-09-09 05:47:10 |     0.41 |
| 2024-09-09 05:48:10 |     0.00 |
| 2024-09-09 05:49:10 |     0.00 |
| 2024-09-09 05:50:11 |     0.00 |
| 2024-09-09 05:51:10 |     0.00 |
| 2024-09-09 05:52:10 |     0.41 |
| 2024-09-09 05:53:10 |     0.00 |
| 2024-09-09 05:54:10 |     0.00 |
| 2024-09-09 05:55:10 |     0.41 |
| 2024-09-09 05:56:11 |     0.00 |
| 2024-09-09 05:57:10 |     0.00 |
| 2024-09-09 05:58:10 |     0.41 |
| 2024-09-09 05:59:10 |     0.00 |
+---------------------+----------+

我喜欢的是一个连续的总和,它将显示 15 分钟、1 小时和 15 分钟内的降雨量,例如:

+---------------------+----------+
| tid                 | regn1min | regn15min | regn1h | regn24h |
+---------------------+----------+
| 2024-09-09 05:00:11 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:01:10 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:02:10 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:03:10 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:04:10 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:05:11 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:06:10 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:07:10 |     0.00 |      0.00 |   0.00 |    0.00 |
| 2024-09-09 05:08:10 |     0.41 |      0.41 |   0.41 |    0.41 |
| 2024-09-09 05:09:10 |     0.00 |      0.41 |   0.41 |    0.41 |
| 2024-09-09 05:10:11 |     0.00 |      0.41 |   0.41 |    0.41 |
| 2024-09-09 05:11:10 |     0.00 |      0.41 |   0.41 |    0.41 |
| 2024-09-09 05:12:10 |     0.00 |      0.41 |   0.41 |    0.41 |
| 2024-09-09 05:13:10 |     0.00 |      0.41 |   0.41 |    0.41 |
| 2024-09-09 05:14:10 |     0.41 |      0.82 |   0.82 |    0.82 |
| 2024-09-09 05:15:10 |     0.00 |      0.82 |   0.82 |    0.82 |
| 2024-09-09 05:16:11 |     0.00 |      0.82 |   0.82 |    0.82 |
| 2024-09-09 05:17:10 |     0.00 |      0.82 |   0.82 |    etc  |
| 2024-09-09 05:18:10 |     0.00 |      0.82 |   0.82 |    vvvv |
| 2024-09-09 05:19:10 |     0.00 |      0.82 |   0.82 |    0.00 |
| 2024-09-09 05:20:10 |     0.00 |      0.82 |   0.82 |    0.00 |
| 2024-09-09 05:21:10 |     0.41 |      1.23 |   1.23 |    0.00 |
| 2024-09-09 05:22:11 |     0.00 |      1.23 |   1.23 |    0.00 |
| 2024-09-09 05:23:10 |     0.00 |      0.82 |   1.23 |    0.00 |
| 2024-09-09 05:24:10 |     0.00 |      0.82 |   1.23 |    0.00 |
| 2024-09-09 05:25:10 |     0.41 |      1.23 |   1.64 |    0.00 |
| 2024-09-09 05:26:10 |     0.00 |      1.23 |   1.64 |    0.00 |
| 2024-09-09 05:27:10 |     0.00 |      1.23 |   1.64 |    0.00 |
| 2024-09-09 05:28:10 |     0.00 |      0.82 |   1.64 |    0.00 |
| 2024-09-09 05:29:10 |     0.41 |      1.23 |   2.05 |    0.00 |
| 2024-09-09 05:30:10 |     0.00 |      1.23 |   2.05 |    0.00 |
| 2024-09-09 05:31:10 |     0.00 |      1.23 |   2.05 |    0.00 |
| 2024-09-09 05:32:10 |     0.41 |      1.64 |   2.46 |    0.00 |
| 2024-09-09 05:33:10 |     0.00 |      1.64 |   2.46 |    0.00 |
| 2024-09-09 05:34:11 |     0.00 |      1.64 |   2.26 |    0.00 |
| 2024-09-09 05:35:10 |     0.41 |      1.64 |   2.67 |    0.00 |
| 2024-09-09 05:36:10 |     0.00 |      1.64 |   2.67 |    0.00 |
| 2024-09-09 05:37:10 |     0.41 |      2.05 |   3.08 |    0.00 |
| 2024-09-09 05:38:10 |     0.00 |      1.64 |   3.08 |    0.00 |
| 2024-09-09 05:39:11 |     0.00 |      2.05 |   3.08 |    0.00 |
| 2024-09-09 05:40:10 |     0.00 |      2.05 |   3.08 |    0.00 |
| 2024-09-09 05:41:10 |     0.00 |      1.64 |   3.08 |    0.00 |
| 2024-09-09 05:42:10 |     0.00 |      1.64 |   3.08 |    0.00 |
| 2024-09-09 05:43:10 |     0.41 |      2.05 |   3.49 |    0.00 |
| 2024-09-09 05:44:10 |     0.00 |      1.64 |   3.49 |    0.00 |
| 2024-09-09 05:45:11 |     0.00 |      1.64 |   3.49 |    0.00 |
| 2024-09-09 05:46:10 |     0.00 |      1.23 |   3.49 |    0.00 |
| 2024-09-09 05:47:10 |     0.41 |      1.64 |   3.90 |    0.00 |
| 2024-09-09 05:48:10 |     0.00 |      1.23 |   3.90 |    0.00 |
| 2024-09-09 05:49:10 |     0.00 |      1.23 |   3.90 |    0.00 |
| 2024-09-09 05:50:11 |     0.00 |      1.23 |   3.90 |    0.00 |
| 2024-09-09 05:51:10 |     0.00 |      0.82 |   3.90 |    0.00 |
| 2024-09-09 05:52:10 |     0.41 |      1.23 |   4.31 |    0.00 |
| 2024-09-09 05:53:10 |     0.00 |      1.23 |   4.31 |    0.00 |
| 2024-09-09 05:54:10 |     0.00 |      1.23 |   4.31 |    0.00 |
| 2024-09-09 05:55:10 |     0.41 |      1.64 |   4.72 |    0.00 |
| 2024-09-09 05:56:11 |     0.00 |      1.64 |   4.72 |    0.00 |
| 2024-09-09 05:57:10 |     0.00 |      1.64 |   4.72 |    0.00 |
| 2024-09-09 05:58:10 |     0.41 |      1.64 |   5.13 |    0.00 |
| 2024-09-09 05:59:10 |     0.00 |      2.05 |   5.31 |    0.00 |
+---------------------+----------+

以上为手工计算,可能存在错误。 24小时并不完整——懒惰的我。

还没有尝试太多,因为我是 SQL 新手。

sum mariadb intervals running-count
1个回答
0
投票

为此,您可以使用带窗框的总和。 如果您假设每分钟有一行,则对于 15 分钟的运行总计,您可以仅使用前面 14 行的范围:

select tid, regn1min,
    sum(regn1min) over (order by tid rows between 14 preceding and current row) regn15min

1 小时和 24 小时也是如此。

如果您明确只想包含 15 分钟,则需要使用范围。 mariadb 仅支持按数字排序时的范围,因此您需要将时间转换为数字(距某个固定点以秒为单位的偏移量):

select tid, regn1min,
    sum(regn1min) over (order by timestampdiff(second,'2025-01-01 00:00:00',tid) range between 870 preceding and current row) regn15min

(870 秒是 14 分 30 秒,考虑到您的时间略有差异)。

在 mysql 中,您可以将范围与日期时间一起使用,例如

over (order by tid range between interval 870 second preceding and current row
,但这在 mariadb 中尚不可能。

© www.soinside.com 2019 - 2024. All rights reserved.