MySQL GROUP BY期末

时间:2017-08-15 11:11:46

标签: mysql datetime group-by

我试图平均价值'列分为4小时结束时段(凌晨4点,上午8点,下午12点)。我使用的是MySQL 5.7版。

示例表和数据如下:

CREATE TABLE TestData(
    Timestamp DATETIME,
    Value FLOAT
);


INSERT INTO TestData
(Timestamp, Value)
VALUES
('2017-08-15 00:00:00', RAND()),
('2017-08-15 00:15:00', RAND()),
('2017-08-15 00:30:00', RAND()),
('2017-08-15 00:45:00', RAND()),
('2017-08-15 01:00:00', RAND()),
('2017-08-15 01:15:00', RAND()),
('2017-08-15 01:30:00', RAND()),
('2017-08-15 01:45:00', RAND()),
('2017-08-15 02:00:00', RAND()),
('2017-08-15 02:15:00', RAND()),
('2017-08-15 02:30:00', RAND()),
('2017-08-15 02:45:00', RAND()),
('2017-08-15 03:00:00', RAND()),
('2017-08-15 03:15:00', RAND()),
('2017-08-15 03:30:00', RAND()),
('2017-08-15 03:45:00', RAND()),
('2017-08-15 04:00:00', RAND()),
('2017-08-15 04:15:00', RAND()),
('2017-08-15 04:30:00', RAND()),
('2017-08-15 04:45:00', RAND()),
('2017-08-15 05:00:00', RAND()),
('2017-08-15 05:15:00', RAND()),
('2017-08-15 05:30:00', RAND()),
('2017-08-15 05:45:00', RAND()),
('2017-08-15 06:00:00', RAND()),
('2017-08-15 06:15:00', RAND()),
('2017-08-15 06:30:00', RAND()),
('2017-08-15 06:45:00', RAND()),
('2017-08-15 07:00:00', RAND()),
('2017-08-15 07:15:00', RAND()),
('2017-08-15 07:30:00', RAND()),
('2017-08-15 07:45:00', RAND()),
('2017-08-15 08:00:00', RAND()),
('2017-08-15 08:15:00', RAND()),
('2017-08-15 08:30:00', RAND()),
('2017-08-15 08:45:00', RAND()),
('2017-08-15 09:00:00', RAND()),
('2017-08-15 09:15:00', RAND()),
('2017-08-15 09:30:00', RAND()),
('2017-08-15 09:45:00', RAND()),
('2017-08-15 10:00:00', RAND()),
('2017-08-15 10:15:00', RAND()),
('2017-08-15 10:30:00', RAND()),
('2017-08-15 10:45:00', RAND()),
('2017-08-15 11:00:00', RAND()),
('2017-08-15 11:15:00', RAND()),
('2017-08-15 11:30:00', RAND()),
('2017-08-15 11:45:00', RAND()),
('2017-08-15 12:00:00', RAND()),
('2017-08-15 12:15:00', RAND()),
('2017-08-15 12:30:00', RAND()),
('2017-08-15 12:45:00', RAND()),
('2017-08-15 12:45:00', RAND());

我想要舍入到下一个时间段(因此使用大于)。 当我运行以下查询时:

SELECT
    Timestamp,
    ROUND(AVG(Value), 3) AS Value
FROM
    TestData
WHERE
    Timestamp > '2017-08-15 00:00:00' AND Timestamp <= '2017-08-15 12:00:00'
GROUP BY
    UNIX_TIMESTAMP( Timestamp ) DIV 14400; -- 14400 seconds = 4 hours

返回:

+---------------------+-------+
| Timestamp           | Value |
+---------------------+-------+
| 2017-08-15 00:15:00 | 0.244 |
| 2017-08-15 01:00:00 | 0.478 |
| 2017-08-15 05:00:00 | 0.439 |
| 2017-08-15 09:00:00 | 0.499 |
+---------------------+-------+
4 rows in set (0.00 sec)

我想要的是什么:

+---------------------+-------+
| Timestamp           | Value |
+---------------------+-------+
| 2017-08-15 04:00:00 | 0.244 |
| 2017-08-15 08:00:00 | 0.478 |
| 2017-08-15 12:00:00 | 0.439 |
+---------------------+-------+
3 rows in set (0.00 sec)

有人可以告诉我这是怎么回事吗?

感谢。

1 个答案:

答案 0 :(得分:1)

<强>查询

DataFrame

<强>结果

In [39]: df.sum().to_frame('sum').T
Out[39]:
             A           B          C          D
sum  2358458.2  1509979.49  2766063.9  2370164.7