如何按星期和小时检索4周移动平均线?

时间:2017-10-16 04:13:20

标签: mysql

我希望在一系列日期中返回一周中一天中的小时的滚动/移动平均值,过去几周的范围。

范围内每个日期的回顾都有一个偏移和一个跨周。对于范围中的每个日期,我想从前几周中提取值,使用一周的开始周数,以及过去一周的数周。

例如,10月1日 - 10月7日,回顾4周,从1周开始。

10月1日至10月7日的每一天,回顾4周的时间,从一周开始,并记录每个小时的计数总和,每天的同一天(1-7) ,在回顾范围内。

然后我们可以用它来做一些数学计算,在这种情况下,得到平均入场密度,或者该范围内每个日期的每周每小时的平均入场次数。

从该范围内的10月1日开始。

这应该产生24行,包括:

日期栏填写于10月1日(2017-10-01),

一天的专栏,(周日1),

一个计数列,每个小时桶的条目总数,对于每个dayofweek(1)的实例,在回溯范围内(4周)

在10月1日返回的一系列星期日(星期日)是(尤达税)。

因此,需要从9月3日,10日,17日和24日开始每小时的参赛作品数,并计算当天每个小时的平均数,超过这4个星期日。 10月1日上午10点的响应值将是3日,10日,17日和24日上午10点的行数之和。

那是10月1日。

对于该范围内的10月2日,它将拉出24行,日期列为2017-10-02,dayofweek列(星期一为2),每个小时的平均距离为回溯距离,其中包括:  9月4日,  9月11日,  9月18日

选择HOUR(日期栏)的计数,从每一天开始,总和将除以每个小时段具有值的回顾范围内的天数。

架构设置:

CREATE TABLE `myTable` (
  `datetimecolumn` datetime<br/>
) 

INSERT INTO MyTable
    (`datetimecolumn`)
VALUES
    ('2015-08-01 00:01:00'),
    ('2015-08-01 00:21:00'),
    ('2015-08-01 01:25:00'),
    ('2015-08-01 01:39:00'),
    ('2015-08-01 02:11:00'),
    ('2015-08-01 03:01:00'),

    ## more here, several randomly timed entries in most hours of each day
每月数千条参赛作品。

    ('2017-10-16 01:01:00'),
    ('2017-10-16 02:11:00'),
    ('2017-10-16 02:21:00'),
    ('2017-10-16 03:01:00'),
    ('2017-10-16 05:43:00'),
    ('2017-10-16 06:21:00')
;

2017-09-30至2017-10-02所需结果:

|     Date   | DOW | hour_of_day | Avg_Num_Entries | 
|------------|-----|-------------| ----------------| 
| 2017-09-30 |   7 |          00 |              12 | 
| 2017-09-30 |   7 |          01 |              11 | 
| 2017-09-30 |   7 |          02 |              14 | 
| 2017-09-30 |   7 |          03 |              12 | 
| 2017-09-30 |   7 |          04 |              11 | 
| 2017-09-30 |   7 |          05 |              14 | 
( ..24 total rows for DAYOFWEEK 7 )

| 2017-10-01 |   1 |          00 |              12 | 
| 2017-10-01 |   1 |          01 |              11 | 
| 2017-10-01 |   1 |          02 |              14 | 
| 2017-10-01 |   1 |          03 |              12 | 
| 2017-10-01 |   1 |          04 |              11 | 
| 2017-10-01 |   1 |          05 |              14 | 
( ..24 total rows for DAYOFWEEK 1 )

| 2017-10-02 |   2 |          00 |              12 | 
| 2017-10-02 |   2 |          01 |              11 | 
| 2017-10-02 |   2 |          02 |              14 | 
| 2017-10-02 |   2 |          03 |              12 | 
| 2017-10-02 |   2 |          04 |              11 | 
| 2017-10-02 |   2 |          05 |              14 | 
( ..24 total rows for DAYOFWEEK 2 )

我目前正在处理这个问题:

SELECT
  DATE(c.datetimecolum) datetimecolum,
  t.sq_datetimecolum,
  t.sq_hour,
  t.sq_count
FROM
  calls c
  INNER JOIN(
              SELECT
                DATE(datetimecolum) sq_datetimecolum,
                DAYOFWEEK(datetimecolum) sq_dow,
                HOUR(datetimecolum) sq_hour,
                COUNT(*) sq_count
              FROM
                calls
              GROUP BY sq_datetimecolum,sq_dow,sq_hour
            ) AS t
    ON
      DATE(t.sq_datetimecolum) <= DATE_SUB(
          DATE(c.datetimecolum),
          INTERVAL 1 WEEK
      ) AND DATE(t.sq_datetimecolum) >= DATE_SUB(
          DATE(c.datetimecolum),
          INTERVAL 4 WEEK
      ) AND DAYOFWEEK(t.sq_datetimecolum) = DAYOFWEEK(DATE(c.datetimecolum))

WHERE  DATE(c.datetimecolum) >= '2017-08-01' AND DATE(c.datetimecolum) <= '2017-08-02'
GROUP BY DATE(c.datetimecolum),DATE(t.sq_datetimecolum),DAYOFWEEK(t.sq_datetimecolum),t.sq_hour
ORDER BY DATE(c.datetimecolum),t.sq_datetimecolum,t.sq_hour

为2017-08-01至2017-08-02的日期范围生成此输出:

|    datetimecolum     | sq_datetimecolum | sq_hour | sq_count |
|----------------------|------------------|---------|----------|
|       2017-08-01     |    2017-07-04    |       0 |       18 |
|       2017-08-01     |    2017-07-04    |       1 |       11 |
|       2017-08-01     |    2017-07-04    |       2 |        7 |
|       2017-08-01     |    2017-07-04    |       3 |        2 |
|       2017-08-01     |    2017-07-04    |       4 |        3 |
|       2017-08-01     |    2017-07-04    |       5 |        2 |
|       2017-08-01     |    2017-07-04    |       6 |        4 |
|       2017-08-01     |    2017-07-04    |       7 |        1 |
|       2017-08-01     |    2017-07-04    |       8 |        4 |
|       2017-08-01     |    2017-07-04    |       9 |        6 |
|       2017-08-01     |    2017-07-04    |      10 |        6 |
|       2017-08-01     |    2017-07-04    |      11 |        3 |
|       2017-08-01     |    2017-07-04    |      12 |       11 |
|       2017-08-01     |    2017-07-04    |      13 |       21 |
|       2017-08-01     |    2017-07-04    |      14 |       44 |
|       2017-08-01     |    2017-07-04    |      15 |       52 |
|       2017-08-01     |    2017-07-04    |      16 |       32 |
|       2017-08-01     |    2017-07-04    |      17 |       26 |
|       2017-08-01     |    2017-07-04    |      18 |       22 |
|       2017-08-01     |    2017-07-04    |      19 |       25 |
|       2017-08-01     |    2017-07-04    |      20 |       14 |
|       2017-08-01     |    2017-07-04    |      21 |        7 |
|       2017-08-01     |    2017-07-04    |      22 |       10 |
|       2017-08-01     |    2017-07-04    |      23 |        6 |
|       2017-08-01     |    2017-07-11    |       0 |       21 |
|       2017-08-01     |    2017-07-11    |       1 |       21 |
|       2017-08-01     |    2017-07-11    |       2 |       12 |
|       2017-08-01     |    2017-07-11    |       3 |       12 |
|       2017-08-01     |    2017-07-11    |       4 |        3 |
|       2017-08-01     |    2017-07-11    |       5 |        4 |
|       2017-08-01     |    2017-07-11    |       6 |        2 |
|       2017-08-01     |    2017-07-11    |       7 |        3 |
|       2017-08-01     |    2017-07-11    |       8 |        7 |
|       2017-08-01     |    2017-07-11    |      10 |        5 |
|       2017-08-01     |    2017-07-11    |      11 |       14 |
|       2017-08-01     |    2017-07-11    |      12 |       50 |
|       2017-08-01     |    2017-07-11    |      13 |       99 |
|       2017-08-01     |    2017-07-11    |      14 |      117 |
|       2017-08-01     |    2017-07-11    |      15 |      132 |
|       2017-08-01     |    2017-07-11    |      16 |      101 |
|       2017-08-01     |    2017-07-11    |      17 |       89 |
|       2017-08-01     |    2017-07-11    |      18 |       81 |
|       2017-08-01     |    2017-07-11    |      19 |      110 |
|       2017-08-01     |    2017-07-11    |      20 |      120 |
|       2017-08-01     |    2017-07-11    |      21 |      119 |
|       2017-08-01     |    2017-07-11    |      22 |       85 |
|       2017-08-01     |    2017-07-11    |      23 |       35 |
|       2017-08-01     |    2017-07-18    |       0 |       44 |
|       2017-08-01     |    2017-07-18    |       1 |       33 |
|       2017-08-01     |    2017-07-18    |       2 |       11 |
|       2017-08-01     |    2017-07-18    |       3 |       13 |
|       2017-08-01     |    2017-07-18    |       4 |        7 |
|       2017-08-01     |    2017-07-18    |       5 |        2 |
|       2017-08-01     |    2017-07-18    |       6 |        7 |
|       2017-08-01     |    2017-07-18    |       7 |        5 |
|       2017-08-01     |    2017-07-18    |       8 |        7 |
|       2017-08-01     |    2017-07-18    |       9 |       10 |
|       2017-08-01     |    2017-07-18    |      10 |        5 |
|       2017-08-01     |    2017-07-18    |      11 |       16 |
|       2017-08-01     |    2017-07-18    |      12 |       39 |
|       2017-08-01     |    2017-07-18    |      13 |       67 |
|       2017-08-01     |    2017-07-18    |      14 |      128 |
|       2017-08-01     |    2017-07-18    |      15 |      133 |
|       2017-08-01     |    2017-07-18    |      16 |      129 |
|       2017-08-01     |    2017-07-18    |      17 |      120 |
|       2017-08-01     |    2017-07-18    |      18 |      102 |
|       2017-08-01     |    2017-07-18    |      19 |       93 |
|       2017-08-01     |    2017-07-18    |      20 |      112 |
|       2017-08-01     |    2017-07-18    |      21 |       81 |
|       2017-08-01     |    2017-07-18    |      22 |       74 |
|       2017-08-01     |    2017-07-18    |      23 |       43 |
|       2017-08-01     |    2017-07-25    |       0 |       32 |
|       2017-08-01     |    2017-07-25    |       1 |       19 |
|       2017-08-01     |    2017-07-25    |       2 |        9 |
|       2017-08-01     |    2017-07-25    |       3 |        3 |
|       2017-08-01     |    2017-07-25    |       4 |        6 |
|       2017-08-01     |    2017-07-25    |       5 |        8 |
|       2017-08-01     |    2017-07-25    |       6 |        3 |
|       2017-08-01     |    2017-07-25    |       7 |        2 |
|       2017-08-01     |    2017-07-25    |       8 |        3 |
|       2017-08-01     |    2017-07-25    |       9 |        2 |
|       2017-08-01     |    2017-07-25    |      10 |        5 |
|       2017-08-01     |    2017-07-25    |      11 |       15 |
|       2017-08-01     |    2017-07-25    |      12 |       39 |
|       2017-08-01     |    2017-07-25    |      13 |       71 |
|       2017-08-01     |    2017-07-25    |      14 |       84 |
|       2017-08-01     |    2017-07-25    |      15 |      100 |
|       2017-08-01     |    2017-07-25    |      16 |       92 |
|       2017-08-01     |    2017-07-25    |      17 |       58 |
|       2017-08-01     |    2017-07-25    |      18 |       59 |
|       2017-08-01     |    2017-07-25    |      19 |       70 |
|       2017-08-01     |    2017-07-25    |      20 |       80 |
|       2017-08-01     |    2017-07-25    |      21 |       83 |
|       2017-08-01     |    2017-07-25    |      22 |       60 |
|       2017-08-01     |    2017-07-25    |      23 |       40 |
|       2017-08-02     |    2017-07-05    |       0 |        7 |
|       2017-08-02     |    2017-07-05    |       1 |        6 |
|       2017-08-02     |    2017-07-05    |       2 |        4 |
|       2017-08-02     |    2017-07-05    |       3 |        4 |
|       2017-08-02     |    2017-07-05    |       4 |        3 |
|       2017-08-02     |    2017-07-05    |       5 |        1 |
|       2017-08-02     |    2017-07-05    |       6 |        2 |
|       2017-08-02     |    2017-07-05    |       7 |        1 |
|       2017-08-02     |    2017-07-05    |       8 |        3 |
|       2017-08-02     |    2017-07-05    |       9 |        3 |
|       2017-08-02     |    2017-07-05    |      10 |        5 |
|       2017-08-02     |    2017-07-05    |      11 |       13 |
|       2017-08-02     |    2017-07-05    |      12 |       50 |
|       2017-08-02     |    2017-07-05    |      13 |       97 |
|       2017-08-02     |    2017-07-05    |      14 |      125 |
|       2017-08-02     |    2017-07-05    |      15 |      139 |
|       2017-08-02     |    2017-07-05    |      16 |      106 |
|       2017-08-02     |    2017-07-05    |      17 |      106 |
|       2017-08-02     |    2017-07-05    |      18 |       93 |
|       2017-08-02     |    2017-07-05    |      19 |       80 |
|       2017-08-02     |    2017-07-05    |      20 |       86 |
|       2017-08-02     |    2017-07-05    |      21 |       68 |
|       2017-08-02     |    2017-07-05    |      22 |       53 |
|       2017-08-02     |    2017-07-05    |      23 |       26 |
|       2017-08-02     |    2017-07-12    |       0 |       24 |
|       2017-08-02     |    2017-07-12    |       1 |       21 |
|       2017-08-02     |    2017-07-12    |       2 |       16 |
|       2017-08-02     |    2017-07-12    |       3 |        3 |
|       2017-08-02     |    2017-07-12    |       4 |        4 |
|       2017-08-02     |    2017-07-12    |       5 |        5 |
|       2017-08-02     |    2017-07-12    |       6 |        1 |
|       2017-08-02     |    2017-07-12    |       7 |        2 |
|       2017-08-02     |    2017-07-12    |       8 |        5 |
|       2017-08-02     |    2017-07-12    |       9 |       15 |
|       2017-08-02     |    2017-07-12    |      10 |        7 |
|       2017-08-02     |    2017-07-12    |      11 |       12 |
|       2017-08-02     |    2017-07-12    |      12 |       44 |
|       2017-08-02     |    2017-07-12    |      13 |       60 |
|       2017-08-02     |    2017-07-12    |      14 |       73 |
|       2017-08-02     |    2017-07-12    |      15 |      119 |
|       2017-08-02     |    2017-07-12    |      16 |      100 |
|       2017-08-02     |    2017-07-12    |      17 |       87 |
|       2017-08-02     |    2017-07-12    |      18 |       58 |
|       2017-08-02     |    2017-07-12    |      19 |       85 |
|       2017-08-02     |    2017-07-12    |      20 |       96 |
|       2017-08-02     |    2017-07-12    |      21 |       79 |
|       2017-08-02     |    2017-07-12    |      22 |       61 |
|       2017-08-02     |    2017-07-12    |      23 |       25 |
|       2017-08-02     |    2017-07-19    |       0 |       27 |
|       2017-08-02     |    2017-07-19    |       1 |       24 |
|       2017-08-02     |    2017-07-19    |       2 |       20 |
|       2017-08-02     |    2017-07-19    |       3 |       12 |
|       2017-08-02     |    2017-07-19    |       4 |        4 |
|       2017-08-02     |    2017-07-19    |       5 |        4 |
|       2017-08-02     |    2017-07-19    |       6 |        2 |
|       2017-08-02     |    2017-07-19    |       7 |        5 |
|       2017-08-02     |    2017-07-19    |       8 |        4 |
|       2017-08-02     |    2017-07-19    |       9 |        4 |
|       2017-08-02     |    2017-07-19    |      10 |       10 |
|       2017-08-02     |    2017-07-19    |      11 |       11 |
|       2017-08-02     |    2017-07-19    |      12 |       33 |
|       2017-08-02     |    2017-07-19    |      13 |       75 |
|       2017-08-02     |    2017-07-19    |      14 |      107 |
|       2017-08-02     |    2017-07-19    |      15 |       98 |
|       2017-08-02     |    2017-07-19    |      16 |       68 |
|       2017-08-02     |    2017-07-19    |      17 |       71 |
|       2017-08-02     |    2017-07-19    |      18 |       96 |
|       2017-08-02     |    2017-07-19    |      19 |      102 |
|       2017-08-02     |    2017-07-19    |      20 |       86 |
|       2017-08-02     |    2017-07-19    |      21 |       67 |
|       2017-08-02     |    2017-07-19    |      22 |       54 |
|       2017-08-02     |    2017-07-19    |      23 |       29 |
|       2017-08-02     |    2017-07-26    |       0 |       35 |
|       2017-08-02     |    2017-07-26    |       1 |       16 |
|       2017-08-02     |    2017-07-26    |       2 |       11 |
|       2017-08-02     |    2017-07-26    |       3 |        8 |
|       2017-08-02     |    2017-07-26    |       4 |        6 |
|       2017-08-02     |    2017-07-26    |       5 |        5 |
|       2017-08-02     |    2017-07-26    |       6 |        3 |
|       2017-08-02     |    2017-07-26    |       7 |        3 |
|       2017-08-02     |    2017-07-26    |       8 |        7 |
|       2017-08-02     |    2017-07-26    |       9 |        2 |
|       2017-08-02     |    2017-07-26    |      10 |        5 |
|       2017-08-02     |    2017-07-26    |      11 |       11 |
|       2017-08-02     |    2017-07-26    |      12 |       41 |
|       2017-08-02     |    2017-07-26    |      13 |       52 |
|       2017-08-02     |    2017-07-26    |      14 |       76 |
|       2017-08-02     |    2017-07-26    |      15 |       83 |
|       2017-08-02     |    2017-07-26    |      16 |       97 |
|       2017-08-02     |    2017-07-26    |      17 |       71 |
|       2017-08-02     |    2017-07-26    |      18 |       56 |
|       2017-08-02     |    2017-07-26    |      19 |       69 |
|       2017-08-02     |    2017-07-26    |      20 |       45 |
|       2017-08-02     |    2017-07-26    |      21 |       67 |
|       2017-08-02     |    2017-07-26    |      22 |       68 |
|       2017-08-02     |    2017-07-26    |      23 |       39 |

我很亲密。这将产生每周一天的每小时数量,从4周开始。

我只需要在4天内将这些小时总数相加,然后除以4。

2 个答案:

答案 0 :(得分:1)

此方法使用2个子查询:

子查询1 通过在指定日期范围内形成日期,星期几和小时的唯一行来控制最终输出。该日期范围使用该范围的起始日期的参数,即在下一个子查询中使用相同的参数。 ( nb:这里假设所有必需的天数和小时数将出现在数据中。如果没有,则需要另一种方法来提供所需的所有天数和小时数

子查询2 从上面使用的起点向后过滤,可以通过参数控制若干周。然后按日周和小时汇总此日期范围。

然后在每周和每小时加入2个子查询,以便平均值与ate,dayofweek和hour对齐。请注意,每个子查询中使用的日期范围是互斥的,因此计数是准确的。

SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE `myTable` (
  `id` mediumint(8) unsigned NOT NULL auto_increment,
  `start_time` datetime,
  PRIMARY KEY (`id`)
) AUTO_INCREMENT=1;

INSERT INTO MyTable
    (`start_time`)
VALUES
('2017-08-05 00:01:00'),
('2017-08-05 00:15:00'),
('2017-08-05 00:29:00'),
('2017-08-05 01:01:00'),
('2017-08-05 01:15:00'),
('2017-08-05 01:29:00'),
('2017-08-05 02:01:00'),
('2017-08-05 02:15:00'),
('2017-08-05 02:29:00'),
('2017-08-05 03:01:00'),
('2017-08-05 03:15:00'),
('2017-08-05 03:29:00'),
('2017-08-05 04:01:00'),
('2017-08-05 04:15:00'),
('2017-08-05 04:29:00'),
('2017-08-05 05:01:00'),
('2017-08-05 05:15:00'),
('2017-08-05 05:29:00'),
('2017-08-05 06:01:00'),
('2017-08-05 06:15:00'),
('2017-08-05 06:29:00'),
('2017-08-05 07:01:00'),
('2017-08-05 07:15:00'),
('2017-08-05 07:29:00'),
('2017-08-05 08:01:00'),
('2017-08-05 08:15:00'),
('2017-08-05 08:29:00'),
('2017-08-05 09:01:00'),
('2017-08-05 09:15:00'),
('2017-08-05 09:29:00'),
('2017-08-05 10:01:00'),
('2017-08-05 10:15:00'),
('2017-08-05 10:29:00'),
('2017-08-05 11:01:00'),
('2017-08-05 11:15:00'),
('2017-08-05 11:29:00'),
('2017-08-05 12:01:00'),
('2017-08-05 12:15:00'),
('2017-08-05 12:29:00'),
('2017-08-05 13:01:00'),
('2017-08-05 13:15:00'),
('2017-08-05 13:29:00'),
('2017-08-05 14:01:00'),
('2017-08-05 14:15:00'),
('2017-08-05 14:29:00'),
('2017-08-05 15:01:00'),
('2017-08-05 15:15:00'),
('2017-08-05 15:29:00'),
('2017-08-05 16:01:00'),
('2017-08-05 16:15:00'),
('2017-08-05 16:29:00'),
('2017-08-05 17:01:00'),
('2017-08-05 17:15:00'),
('2017-08-05 17:29:00'),
('2017-08-05 18:01:00'),
('2017-08-05 18:15:00'),
('2017-08-05 18:29:00'),
('2017-08-05 19:01:00'),
('2017-08-05 19:15:00'),
('2017-08-05 19:29:00'),
('2017-08-05 20:01:00'),
('2017-08-05 20:15:00'),
('2017-08-05 20:29:00'),
('2017-08-05 21:01:00'),
('2017-08-05 21:15:00'),
('2017-08-05 21:29:00'),
('2017-08-05 22:01:00'),
('2017-08-05 22:15:00'),
('2017-08-05 22:29:00'),
('2017-08-05 23:01:00'),
('2017-08-05 23:15:00'),
('2017-08-05 23:29:00'),
('2017-08-06 00:01:00'),
('2017-08-06 00:15:00'),
('2017-08-06 00:29:00'),
('2017-08-06 01:01:00'),
('2017-08-06 01:15:00'),
('2017-08-06 01:29:00'),
('2017-08-06 02:01:00'),
('2017-08-06 02:15:00'),
('2017-08-06 02:29:00'),
('2017-08-06 03:01:00'),
('2017-08-06 03:15:00'),
('2017-08-06 03:29:00'),
('2017-08-06 04:01:00'),
('2017-08-06 04:15:00'),
('2017-08-06 04:29:00'),
('2017-08-06 05:01:00'),
('2017-08-06 05:15:00'),
('2017-08-06 05:29:00'),
('2017-08-06 06:01:00'),
('2017-08-06 06:15:00'),
('2017-08-06 06:29:00'),
('2017-08-06 07:01:00'),
('2017-08-06 07:15:00'),
('2017-08-06 07:29:00'),
('2017-08-06 08:01:00'),
('2017-08-06 08:15:00'),
('2017-08-06 08:29:00'),
('2017-08-06 09:01:00'),
('2017-08-06 09:15:00'),
('2017-08-06 09:29:00'),
('2017-08-06 10:01:00'),
('2017-08-06 10:15:00'),
('2017-08-06 10:29:00'),
('2017-08-06 11:01:00'),
('2017-08-06 11:15:00'),
('2017-08-06 11:29:00'),
('2017-08-06 12:01:00'),
('2017-08-06 12:15:00'),
('2017-08-06 12:29:00'),
('2017-08-06 13:01:00'),
('2017-08-06 13:15:00'),
('2017-08-06 13:29:00'),
('2017-08-06 14:01:00'),
('2017-08-06 14:15:00'),
('2017-08-06 14:29:00'),
('2017-08-06 15:01:00'),
('2017-08-06 15:15:00'),
('2017-08-06 15:29:00'),
('2017-08-06 16:01:00'),
('2017-08-06 16:15:00'),
('2017-08-06 16:29:00'),
('2017-08-06 17:01:00'),
('2017-08-06 17:15:00'),
('2017-08-06 17:29:00'),
('2017-08-06 18:01:00'),
('2017-08-06 18:15:00'),
('2017-08-06 18:29:00'),
('2017-08-06 19:01:00'),
('2017-08-06 19:15:00'),
('2017-08-06 19:29:00'),
('2017-08-06 20:01:00'),
('2017-08-06 20:15:00'),
('2017-08-06 20:29:00'),
('2017-08-06 21:01:00'),
('2017-08-06 21:15:00'),
('2017-08-06 21:29:00'),
('2017-08-06 22:01:00'),
('2017-08-06 22:15:00'),
('2017-08-06 22:29:00'),
('2017-08-06 23:01:00'),
('2017-08-06 23:15:00'),
('2017-08-06 23:29:00'),
('2017-08-03 00:01:00'),
('2017-08-03 00:15:00'),
('2017-08-03 00:29:00'),
('2017-08-03 01:01:00'),
('2017-08-03 01:15:00'),
('2017-08-03 01:29:00'),
('2017-08-03 02:01:00'),
('2017-08-03 02:15:00'),
('2017-08-03 02:29:00'),
('2017-08-03 03:01:00'),
('2017-08-03 03:15:00'),
('2017-08-03 03:29:00'),
('2017-08-03 04:01:00'),
('2017-08-03 04:15:00'),
('2017-08-03 04:29:00'),
('2017-08-03 05:01:00'),
('2017-08-03 05:15:00'),
('2017-08-03 05:29:00'),
('2017-08-03 06:01:00'),
('2017-08-03 06:15:00'),
('2017-08-03 06:29:00'),
('2017-08-03 07:01:00'),
('2017-08-03 07:15:00'),
('2017-08-03 07:29:00'),
('2017-08-03 08:01:00'),
('2017-08-03 08:15:00'),
('2017-08-03 08:29:00'),

('2017-08-12 00:01:00'),
('2017-08-12 00:15:00'),
('2017-08-12 00:29:00'),
('2017-08-12 01:01:00'),
('2017-08-12 01:15:00'),
('2017-08-12 01:29:00'),
('2017-08-12 02:01:00'),
('2017-08-12 02:15:00'),
('2017-08-12 02:29:00'),
('2017-08-12 03:01:00'),
('2017-08-12 03:15:00'),
('2017-08-12 03:29:00'),
('2017-08-12 04:01:00'),
('2017-08-12 04:15:00'),
('2017-08-12 04:29:00'),
('2017-08-12 05:01:00'),
('2017-08-12 05:15:00'),
('2017-08-12 05:29:00'),
('2017-08-12 06:01:00'),
('2017-08-12 06:15:00'),
('2017-08-12 06:29:00'),
('2017-08-12 07:01:00'),
('2017-08-12 07:15:00'),
('2017-08-12 07:29:00'),
('2017-08-12 08:01:00'),
('2017-08-12 08:15:00'),
('2017-08-12 08:29:00'),
('2017-08-12 09:01:00'),
('2017-08-12 09:15:00'),
('2017-08-12 09:29:00'),
('2017-08-12 10:01:00'),
('2017-08-12 10:15:00'),
('2017-08-12 10:29:00'),
('2017-08-12 11:01:00'),
('2017-08-12 11:15:00'),
('2017-08-12 11:29:00'),
('2017-08-12 12:01:00'),
('2017-08-12 12:15:00'),
('2017-08-12 12:29:00'),
('2017-08-12 13:01:00'),
('2017-08-12 13:15:00'),
('2017-08-12 13:29:00'),
('2017-08-12 14:01:00'),
('2017-08-12 14:15:00'),
('2017-08-12 14:29:00'),
('2017-08-12 15:01:00'),
('2017-08-12 15:15:00'),
('2017-08-12 15:29:00'),
('2017-08-12 16:01:00'),
('2017-08-12 16:15:00'),
('2017-08-12 16:29:00'),
('2017-08-12 17:01:00'),
('2017-08-12 17:15:00'),
('2017-08-12 17:29:00'),
('2017-08-12 18:01:00'),
('2017-08-12 18:15:00'),
('2017-08-12 18:29:00'),
('2017-08-12 19:01:00'),
('2017-08-12 19:15:00'),
('2017-08-12 19:29:00'),
('2017-08-12 20:01:00'),
('2017-08-12 20:15:00'),
('2017-08-12 20:29:00'),
('2017-08-12 21:01:00'),
('2017-08-12 21:15:00'),
('2017-08-12 21:29:00'),
('2017-08-12 22:01:00'),
('2017-08-12 22:15:00'),
('2017-08-12 22:29:00'),
('2017-08-12 23:01:00'),
('2017-08-12 23:15:00'),
('2017-08-12 23:29:00'),
('2017-08-13 00:01:00'),
('2017-08-13 00:15:00'),
('2017-08-13 00:29:00'),
('2017-08-13 01:01:00'),
('2017-08-13 01:15:00'),
('2017-08-13 01:29:00'),
('2017-08-13 02:01:00'),
('2017-08-13 02:15:00'),
('2017-08-13 02:29:00'),
('2017-08-13 03:01:00'),
('2017-08-13 03:15:00'),
('2017-08-13 03:29:00'),
('2017-08-13 04:01:00'),
('2017-08-13 04:15:00'),
('2017-08-13 04:29:00'),
('2017-08-13 05:01:00'),
('2017-08-13 05:15:00'),
('2017-08-13 05:29:00'),
('2017-08-13 06:01:00'),
('2017-08-13 06:15:00'),
('2017-08-13 06:29:00'),
('2017-08-13 07:01:00'),
('2017-08-13 07:15:00'),
('2017-08-13 07:29:00'),
('2017-08-13 08:01:00'),
('2017-08-13 08:15:00'),
('2017-08-13 08:29:00'),
('2017-08-13 09:01:00'),
('2017-08-13 09:15:00'),
('2017-08-13 09:29:00'),
('2017-08-13 10:01:00'),
('2017-08-13 10:15:00'),
('2017-08-13 10:29:00'),
('2017-08-13 11:01:00'),
('2017-08-13 11:15:00'),
('2017-08-13 11:29:00'),
('2017-08-13 12:01:00'),
('2017-08-13 12:15:00'),
('2017-08-13 12:29:00'),
('2017-08-13 13:01:00'),
('2017-08-13 13:15:00'),
('2017-08-13 13:29:00'),
('2017-08-13 14:01:00'),
('2017-08-13 14:15:00'),
('2017-08-13 14:29:00'),
('2017-08-13 15:01:00'),
('2017-08-13 15:15:00'),
('2017-08-13 15:29:00'),
('2017-08-13 16:01:00'),
('2017-08-13 16:15:00'),
('2017-08-13 16:29:00'),
('2017-08-13 17:01:00')
;

<强>查询

set @start_at := '2017-08-12'
set @num_weeks:= 4
set @sub_nw_st := date_sub(@start_at, INTERVAL (7 * @num_weeks) DAY)

select
     c.dt, c.dow, c.hr
     , coalesce(h.c_count,0) + c.c_count c_count
     , coalesce(h.dt_count,0) + 1 dt_count 
     , (coalesce(h.c_count,0) + c.c_count) / (coalesce(h.dt_count,0) + 1) Avg_Num_Entries
from (
    select
        date(start_time) dt
      , dayofweek(start_time) dow
      , hour(start_time) hr
      , count(*) c_count
    from MyTable
    where start_time >= @start_at and start_time < date_add(@start_at,INTERVAL 1 WEEK)
    group by
          dt, dow, hr
    ) c
LEFT JOIN (
      select
          dayofweek(start_time) dow
        , hour(start_time) hr
        , count(distinct date(start_time)) dt_count
        , count(*) c_count
      from MyTable
      where start_time >= @sub_nw_st and start_time < @start_at
      group by
            dow, hr
      ) h
          ON c.dow = h.dow and c.hr = h.hr

<强> Results

|         dt | dow | hr | c_count | dt_count | Avg_Num_Entries |
|------------|-----|----|---------|----------|-----------------|
| 2017-08-12 |   7 |  0 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  1 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  2 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  3 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  4 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  5 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  6 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  7 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  8 |       6 |        2 |               3 |
| 2017-08-12 |   7 |  9 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 10 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 11 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 12 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 13 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 14 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 15 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 16 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 17 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 18 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 19 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 20 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 21 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 22 |       6 |        2 |               3 |
| 2017-08-12 |   7 | 23 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  0 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  1 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  2 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  3 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  4 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  5 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  6 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  7 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  8 |       6 |        2 |               3 |
| 2017-08-13 |   1 |  9 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 10 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 11 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 12 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 13 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 14 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 15 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 16 |       6 |        2 |               3 |
| 2017-08-13 |   1 | 17 |       4 |        2 |               2 |

答案 1 :(得分:0)

更新:这是我目前的解决方案,基于已接受的答案。

SET @start_at := '2017-08-01';
  SET @end_at := '2017-08-02';
  SET @lookback_offset := 1;
  SET @lookback_span := 4;

  SELECT
    c.*,
    count(distinct date(h.dt_col)) days_counted,
    count(hour(h.dt_col)) dowhodt,
    count(hour(h.dt_col))/count(distinct date(h.dt_col)) dowhodma
  FROM
    (SELECT
       DATE(dt_col) dt,
       DAYOFWEEK(dt_col) dow,
       HOUR(dt_col) hr
     FROM
       MyTable
     GROUP BY dt , dow , hr) c
  LEFT JOIN (
    SELECT *
    FROM MyTable
  ) h ON
        (DAYOFWEEK(DATE(h.dt_col)) = c.dow)
        AND
        (HOUR(h.dt_col) = c.hr)
        AND
        (DATE(h.dt_col) >= DATE_SUB(c.dt, INTERVAL @lookback_span WEEK))
        AND
        (DATE(h.dt_col) <= DATE_SUB(c.dt, INTERVAL @lookback_offset WEEK))


  WHERE DATE( c.dt ) >= @start_at AND DATE( c.dt ) <= @end_at
  GROUP BY  dt,dow,hr