我有一个包含以下结构的表
id date timestamp licenseid storeid deviceid value
1 2015-06-12 17:36:15 lic0001 1 0add 52
2 2015-06-12 17:36:15 lic0002 1 0add 54
3 2015-06-12 17:36:15 lic0003 1 0add 53
4 2015-06-12 17:36:21 lic0001 1 0add 54
5 2015-06-12 17:36:21 lic0002 1 0add 59
6 2015-06-12 17:36:21 lic0003 1 0add 62
7 2015-06-12 17:36:21 lic0004 1 0add 55
8 2015-06-12 17:36:15 lic0001 1 0bdd 53
9 2015-06-12 17:36:15 lic0002 1 0bdd 52
10 2015-06-12 17:36:15 lic0003 1 0bdd 52
我需要检查是否有任何许可证已停止报告值。我想到的方法是将time_stamps分成15分钟,然后检查是否缺少任何许可证的数据,持续时间更长,然后深潜。
需要帮助查询,可以从00:00开始每隔15分钟按时间戳分组,并显示该持续时间内的值计数。我已经尝试了以下查询,但时间戳没有显示正确的值。它只显示一行,时间键为0。
SELECT count(*), round(unix_timestamp(time_stamp)/(15*60)) AS timekey
FROM data
GROUP BY timekey;
PS:我已按照上述说明使用此帖子中的此查询,但查询为时间键返回0:Group mysql query by 15 min intervals
答案 0 :(得分:0)
UNIX_TIMESTAMP()
不适用于time
参数。它适用于datetime
。所以,一种方法是:
SELECT count(*),
round(unix_timestamp(addtime(date(0), time_stamp) )/(15*60)) AS timekey
FROM data
GROUP BY timekey;
编辑:
你总是可以使用蛮力方法:
SELECT COUNT(*), hour(timestamp) as hr, floor(minute(timestamp) / 15) as minute
FROM data
GROUP BY hr, minute;
答案 1 :(得分:0)
我目前没有MySQL来测试我提出的解决方案,但您可以尝试使用CASE语句吗?
SELECT (CASE
WHEN MINUTE(timestamp) BETWEEN 0 AND 15 THEN 1
WHEN MINUTE(timestamp) BETWEEN 16 AND 30 THEN 2
WHEN MINUTE(timestamp) BETWEEN 31 AND 45 THEN 3
ELSE 4 END) AS MINUTEGROUP
然后在MINUTEGROUP上分组
答案 2 :(得分:0)
使用
create table foo (id int, date date, time_stamp time);
insert into foo (id, date, time_stamp) values (1, '2015-06-12', '17:26:15');
insert into foo (id, date, time_stamp) values (2, '2015-06-12', '17:26:15');
insert into foo (id, date, time_stamp) values (3, '2015-06-12', '17:26:15');
insert into foo (id, date, time_stamp) values (4, '2015-06-12', '17:36:21');
insert into foo (id, date, time_stamp) values (5, '2015-06-12', '17:36:21');
insert into foo (id, date, time_stamp) values (6, '2015-06-12', '17:36:21');
insert into foo (id, date, time_stamp) values (7, '2015-06-12', '18:36:21');
insert into foo (id, date, time_stamp) values (8, '2015-06-12', '18:36:15');
insert into foo (id, date, time_stamp) values (9, '2015-06-12', '18:36:15');
insert into foo (id, date, time_stamp) values (10, '2015-06-12', '18:36:15');
看起来像
select
date,
count(*),
min(addtime(date, time_stamp)) min_time,
max(addtime(date, time_stamp)) max_time,
date_add(date, interval timestampdiff(second, date, addtime(date, time_stamp)) div 15 * 15 minute) start_time
from
foo
group by
date,
timestampdiff(minute, date, addtime(date, time_stamp)) div 15