我知道有很多关于运行总计的论坛问题,但不知怎的,我不知道如何应用于我的例子。如果有人可以提供帮助,那就太感激了。
我正在尝试按“ID”计算每小时运行的“速度”分区总数。我该如何处理?我正在使用vertica数据库。谢谢!
DATETIME,ID,speed
05-12-15 0:15,1,40
05-12-15 0:30,1,61
05-12-15 0:45,1,66
05-12-15 1:00,1,48
05-12-15 1:15,1,66
05-12-15 1:30,1,50
05-12-15 1:45,1,47
05-12-15 2:00,1,70
05-12-15 2:15,1,42
05-12-15 2:30,1,58
05-12-15 2:45,1,74
05-12-15 3:00,1,43
05-12-15 3:15,1,57
05-12-15 3:30,1,69
05-12-15 3:45,1,52
05-12-15 4:00,1,43
05-12-15 0:15,2,48
05-12-15 0:30,2,73
05-12-15 0:45,2,41
05-12-15 1:00,2,56
05-12-15 1:15,2,65
05-12-15 1:30,2,76
05-12-15 1:45,2,66
05-12-15 2:00,2,49
05-12-15 2:15,2,75
05-12-15 2:30,2,67
05-12-15 2:45,2,76
05-12-15 3:00,2,47
05-12-15 3:15,2,53
05-12-15 3:30,2,76
05-12-15 3:45,2,63
05-12-15 4:00,2,56
编辑: 最终结果应该是这样的:
答案 0 :(得分:1)
您应该能够按ID进行分区并使用range between '1 hour' preceding and current row
。 RANGE
使用ORDER BY
中的值来限制窗口框架。
select datetime,
id,
speed,
sum(speed) over (partition by id
order by datetime desc
RANGE BETWEEN '1 hour' PRECEDING
AND CURRENT ROW) sum_speed_1hour
from mytable
order by id, datetime
另外,请注意日期时间desc,它基于您的示例是您正在寻找的。 p>
答案 1 :(得分:0)
您可以尝试以下操作:
select
TIME_SLICE(datetime, 1, 'HOUR'),
sum(speed)
from mytable
group by TIME_SLICE(datetime, 1, 'HOUR')
order by TIME_SLICE(datetime, 1, 'HOUR')