[英]MYSQL : Select count group by day with custom starting and ending hours for a day
I like to group results by day,month year. 我喜欢按年,月,年对结果分组。 My issue is that in my business, the day start at 2PM (14:00:00) and end at 8AM (8:00:00). 我的问题是,在我的公司中,一天开始于2PM(14:00:00),结束于8AM(8:00:00)。
The goal here is counting number of unique customers who have registered during a day. 这里的目标是计算一天中注册的唯一客户的数量。
My table look like this: 我的桌子看起来像这样:
id customer join_date leave_date duration
6247 4043035 2013-06-07 14:32:00 2013-06-07 14:57:24 1524
6248 4006087 2013-06-07 14:32:11 2013-06-07 14:41:18 547
6249 4020103 2013-06-07 14:32:30 2013-06-07 15:24:32 3122
6250 4020103 2013-06-07 14:32:30 2013-06-07 14:41:18 528
6251 4020103 2013-06-07 14:32:30 2013-06-07 15:55:33 4983
6252 4049611 2013-06-07 14:34:14 2013-06-07 17:14:25 9611
6253 4049611 2013-06-07 14:34:14 2013-06-07 14:57:15 1381
6254 4046774 2013-06-07 14:36:21 2013-06-07 14:41:18 297
6255 4048402 2013-06-07 14:37:51 2013-06-07 14:41:18 207
6256 4006073 2013-06-07 14:39:54 2013-06-07 14:42:19 145
6257 4022477 2013-06-07 14:40:40 2013-06-07 14:46:44 364
6258 4049923 2013-06-07 14:42:08 2013-06-07 14:57:09 901
6259 4018158 2013-06-07 14:45:05 2013-06-07 14:45:43 38
6260 4010012 2013-06-07 14:45:39 2013-06-07 14:46:44 65
6261 4018158 2013-06-07 14:45:43 2013-06-07 14:53:16 453
With "normal" days, my request look like this: 在“正常”的日子里,我的要求如下所示:
SELECT count(distinct l.customer) nbj,
DAY(l.join_date) jour,
MONTH(l.join_date) mois,
str_to_date(l.join_date,'%Y-%m-%d') ordre
FROM log_waitingtime l
GROUP BY DAY(l.join_date), MONTH(l.join_date)
ORDER BY ordre ASC
It works fine. 工作正常。
I've try many things: http://forums.mysql.com/read.php?10,202789,202807 http://www.artfulsoftware.com/infotree/qrytip.php?id=819 我已经尝试了很多东西: http : //forums.mysql.com/read.php ? 10,202789,202807 http://www.artfulsoftware.com/infotree/qrytip.php?id=819
I think those solution a complicated (I even don't understand everything. 我认为这些解决方案很复杂(我什至不了解所有内容。
Then, is there a more simple way to accomplish that in SQL ? 然后,有没有更简单的方法可以在SQL中完成呢?
I would use this query: 我将使用以下查询:
SELECT
MONTH(l.join_date - INTERVAL 14 HOUR) mois,
DAY(l.join_date - INTERVAL 14 HOUR) jour,
COUNT(distinct l.customer) nbj,
str_to_date(l.join_date - INTERVAL 14 HOUR,'%Y-%m-%d') ordre
FROM log_waitingtime l
GROUP BY
MONTH(l.join_date - INTERVAL 14 HOUR),
DAY(l.join_date - INTERVAL 14 HOUR)
ORDER BY
ordre ASC
Try this it will display the customers who have registered during a day between 2PM (14:00:00) 8AM (8:00:00) duration 尝试此操作,它将显示在2PM(14:00:00)8AM(8:00:00)持续时间内的一天中注册的客户
SELECT q.* FROM(
SELECT COUNT(DISTINCT l.customer) nbj,
DATE_FORMAT(l.join_date,'%H:%i:%s') TIMEONLY,
DAY(l.join_date) jour,
MONTH(l.join_date) mois,
STR_TO_DATE(l.join_date,'%Y-%m-%d') ordre
FROM log_waitingtime l
GROUP BY DAY(l.join_date), MONTH(l.join_date)
) q WHERE q.TIMEONLY >= '14:00:00' OR q.TIMEONLY <= '08:00:00'
ORDER BY q.jour,q.mois, q.ordre ASC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.