1

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).

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

I think those solution a complicated (I even don't understand everything.

Then, is there a more simple way to accomplish that in SQL ?

4

2 回答 2

1

我会使用这个查询:

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
于 2013-06-30T13:41:30.823 回答
1

试试这个,它会显示在下午 2 点 (14:00:00) 到上午 8 点 (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
于 2013-06-30T13:48:41.633 回答