我有一个超过100,000行的表,其中包含以下列:ID,Time和Boolean 时间列跟踪时间到第二个 我需要一个查询,它会在从表开始到结束的每5分钟时间间隔内找到布尔值= 1的所有实例,然后按时间间隔对计数进行分组。
该表代表4小时的数据,因此我应该得到48行结果 我正在使用MS SQL Server。
我尝试过几种方法,但时间间隔逻辑让我很难过。
编辑:我明白了!SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, timestamp)/5 * 5,0), COUNT(*)
FROM table
WHERE isrepeat = 1
GROUP BY by DATEADD(MINUTE, DATEDIFF(MINUTE, 0, timestamp)/5 * 5,0)
ORDER BY by 1
答案 0 :(得分:2)
这应该做。您可以按时间间隔/ 5对结果进行分组。
select
cast(to_char(Time, 'mi') as int) / 5 * 5 || ' - ' || cast(to_char(Time, 'mi') as int) / 5 * 5 + 5 as "Interval",
count(1)
from tableName
where
Boolean = 1
group by
cast(to_char(Time, 'mi') as int) / 5
答案 1 :(得分:1)
select * from tableName where Time
BETWEEN DATEADD(minute, -5, current_timestamp)
AND current_timestamp