SQL:使用带时间戳的电表数据
SQL: working with timestamped energy meter data
我有一个 table 以十五分钟为间隔的能源使用数据:
METER
TIMESTAMP
KWH
2500396
12/04/2022 23:15
131.01
2500396
12/04/2022 23:30
132.11
2500396
12/04/2022 23:45
125.84
2500396
13/04/2022 00:00
127.27
2500396
13/04/2022 00:15
123.86
2500396
13/04/2022 00:30
114.51
2500396
13/04/2022 00:45
117.7
2500396
13/04/2022 01:00
120.01
我需要计算每小时的能源使用量,例如,2022 年 12 月 4 日第 23 小时的使用量是 12/04/2022 23:15 到 13/04/2022 00:00(注意日期的变化),12/04/2022 的第 0 小时的使用量是从 13/04/2022 00:15 到 13/04/2022 01:00.
的间隔
对于如何在 Oracle 中对此进行简洁编码的建议,我将不胜感激 SQL。
您可以从时间中减去 15 分钟,然后截断到小时的开始,然后按该时间和仪表进行汇总:
SELECT meter,
TRUNC(timestamp - INTERVAL '15' MINUTE, 'HH') AS hour,
SUM(kwh) AS total_kwh
FROM table_name
GROUP BY
meter,
TRUNC(timestamp - INTERVAL '15' MINUTE, 'HH');
其中,对于示例数据:
CREATE TABLE table_name (METER, TIMESTAMP, KWH) AS
SELECT 2500396, DATE '2022-04-12' + INTERVAL '23:15' HOUR TO MINUTE, 131.01 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-12' + INTERVAL '23:30' HOUR TO MINUTE, 132.11 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-12' + INTERVAL '23:45' HOUR TO MINUTE, 125.84 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:00' HOUR TO MINUTE, 127.27 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:15' HOUR TO MINUTE, 123.86 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:30' HOUR TO MINUTE, 114.51 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:45' HOUR TO MINUTE, 117.70 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '01:00' HOUR TO MINUTE, 120.01 FROM DUAL;
输出:
METER
HOUR
TOTAL_KWH
2500396
2022-04-12 23:00:00
516.23
2500396
2022-04-13 00:00:00
476.08
db<>fiddle here
我有一个 table 以十五分钟为间隔的能源使用数据:
METER | TIMESTAMP | KWH |
---|---|---|
2500396 | 12/04/2022 23:15 | 131.01 |
2500396 | 12/04/2022 23:30 | 132.11 |
2500396 | 12/04/2022 23:45 | 125.84 |
2500396 | 13/04/2022 00:00 | 127.27 |
2500396 | 13/04/2022 00:15 | 123.86 |
2500396 | 13/04/2022 00:30 | 114.51 |
2500396 | 13/04/2022 00:45 | 117.7 |
2500396 | 13/04/2022 01:00 | 120.01 |
我需要计算每小时的能源使用量,例如,2022 年 12 月 4 日第 23 小时的使用量是 12/04/2022 23:15 到 13/04/2022 00:00(注意日期的变化),12/04/2022 的第 0 小时的使用量是从 13/04/2022 00:15 到 13/04/2022 01:00.
的间隔对于如何在 Oracle 中对此进行简洁编码的建议,我将不胜感激 SQL。
您可以从时间中减去 15 分钟,然后截断到小时的开始,然后按该时间和仪表进行汇总:
SELECT meter,
TRUNC(timestamp - INTERVAL '15' MINUTE, 'HH') AS hour,
SUM(kwh) AS total_kwh
FROM table_name
GROUP BY
meter,
TRUNC(timestamp - INTERVAL '15' MINUTE, 'HH');
其中,对于示例数据:
CREATE TABLE table_name (METER, TIMESTAMP, KWH) AS
SELECT 2500396, DATE '2022-04-12' + INTERVAL '23:15' HOUR TO MINUTE, 131.01 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-12' + INTERVAL '23:30' HOUR TO MINUTE, 132.11 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-12' + INTERVAL '23:45' HOUR TO MINUTE, 125.84 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:00' HOUR TO MINUTE, 127.27 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:15' HOUR TO MINUTE, 123.86 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:30' HOUR TO MINUTE, 114.51 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '00:45' HOUR TO MINUTE, 117.70 FROM DUAL UNION ALL
SELECT 2500396, DATE '2022-04-13' + INTERVAL '01:00' HOUR TO MINUTE, 120.01 FROM DUAL;
输出:
METER HOUR TOTAL_KWH 2500396 2022-04-12 23:00:00 516.23 2500396 2022-04-13 00:00:00 476.08
db<>fiddle here