You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apache IoTDB 2.0.4表模型中统计每周一指定时段的年数据总和?

问题:计算Apache IoTDB v2.0.4中一年周期内每周一16:00-18:00的传感器数据总和

数据模式如下:

CREATE TABLE sensors (
  device_id STRING TAG,
  temperature FLOAT FIELD
);

尝试使用date_bin函数但未成功限制聚合范围,执行语句:

select date_bin(1w,`time`,'2024-01-01 16:00:00.000') as week_time,
sum(temperature) as temp_sum
from sensors
where device_id = 'HK0897'
and (`time` >= 2024-01-01 00:00:00 and `time` <= 2024-12-31 23:59:59)
group by 1

返回结果为整周数据总和,期望得到一年中每周一16:00-18:00时段的温度总和(共52行)。


解决方案

需在WHERE条件中新增两个过滤规则,精准定位目标时段:

  1. 用dayofweek()筛选周一记录:IoTDB v2.0.4中该函数返回1代表周日,2代表周一
  2. 用hour()筛选16:00-18:00的小时范围

修改后的SQL语句:

select date_bin(1w, `time`, '2024-01-01 16:00:00.000') as week_time,
sum(temperature) as temp_sum
from sensors
where device_id = 'HK0897'
and `time` >= '2024-01-01 00:00:00' 
and `time` <= '2024-12-31 23:59:59'
and dayofweek(`time`) = 2  -- 过滤周一
and hour(`time`) >= 16 
and hour(`time`) < 18  -- 过滤16:00至18:00(不含18:00整)
group by 1

补充细节

  • 若需包含18:00整的记录,可将hour(time) < 18调整为(hour(time) = 18 and minute(time) = 0),或直接用时间范围判断time < '2024-xx-xx 18:00:00'
  • date_bin的起始时间'2024-01-01 16:00:00.000'为周一,确保分组周起始与目标时段匹配,保证每组对应一周的周一数据

内容的提问来源于stack exchange,提问作者Hsin-Yuan Huang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 08:12:33