我有一个MySQL查询,我正在运行以查明在15分钟的时间间隔内,在任何给定时间(任何指定日期等)为用户预订了多少次呼叫。到目前为止,我有:
SELECT
date_action,
DATE_FORMAT(date_action, "%i") AS minute,
CASE
WHEN minute(date_action) BETWEEN 0 and 14 THEN '00'
WHEN minute(date_action) BETWEEN 15 and 29 THEN '15'
WHEN minute(date_action) BETWEEN 30 and 44 THEN '30'
WHEN minute(date_action) BETWEEN 45 and 59 THEN '45'
END AS intervals
FROM
clients
WHERE
HOUR(date_action) = 09
AND DAY(date_action) = 15
AND MONTH(date_action) = 07
AND YEAR(date_action) = 2013
这很好用,但我缺少的神奇成分是分配给每个区间的调用次数的“计数”,所以例如上面的代码产生:
Date_time - Minute - Interval
2013-07-15 09:54:24 - 54 - 45
2013-07-15 09:29:32 - 29 - 15
2013-07-15 09:13:33 - 13 - 00
2013-07-15 09:53:53 - 53 - 45
2013-07-15 09:00:49 - 00 - 00
所以,我将我的会议记录转换为间隔,因为我只希望每个间隔返回1行,然后在该行间隔附加一个列,其中包含该间隔内呼叫次数的计数,即
Interval - Count
45 - 2
15 - 1
00 - 2
我觉得我已经把头撞在这一块砖墙上太久了,所以任何帮助都会非常感激。虽然在查询之后会进行一些后处理,但如果可能的话,我宁愿在SQL中执行此操作。 :)
答案 0 :(得分:3)
您可以按间隔进行分组并选择计数。
SELECT
count(date_action) as count,
CASE
WHEN minute(date_action) BETWEEN 0 and 14 THEN '00'
WHEN minute(date_action) BETWEEN 15 and 29 THEN '15'
WHEN minute(date_action) BETWEEN 30 and 44 THEN '30'
WHEN minute(date_action) BETWEEN 45 and 59 THEN '45'
END AS intervals
FROM
clients
WHERE
HOUR(date_action) = 09
AND DAY(date_action) = 15
AND MONTH(date_action) = 07
AND YEAR(date_action) = 2013
AND rep_id = 28
GROUP BY intervals
答案 1 :(得分:0)
select period, Sum(NumberOfTimes) as NumberOfTimes
From
(Select minute(date_action) % 15 as period, 1 as NumberOfTimes
From Clients
WHERE
HOUR(date_action) = 09
AND DAY(date_action) = 15
AND MONTH(date_action) = 07
AND YEAR(date_action) = 2013
AND rep_id = 28
) dummyName
Group By period
将是另一种方式
%是模数运算符