2017-08-15 103 views
-1

我正在嘗試將「值」列分組到4小時的結束時間段(上午4點,上午8點,下午12點)。我正在使用MySQL版本5.7。MySQL GROUP BY期末

一個實例表和數據是如下:

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

如果我的RAND值不匹配你的嗎?這不是很有用,是嗎? – Strawberry

+0

這些值本身並不重要 - 只要組平均值是正確的 –

回答

1

查詢

SELECT 
    TIMESTAMP 
    , ROUND(AVG(VALUE), 3) AS VALUE 
FROM 
testdata 
WHERE 
    HOUR(TIMESTAMP) >= 4 
AND 
    HOUR(TIMESTAMP) <= 12 
GROUP BY 
FLOOR(HOUR(TIMESTAMP)/4) 

結果

timestamp    Value 
------------------- -------- 
2017-08-15 04:00:00  0.500 
2017-08-15 08:00:00  0.441 
2017-08-15 12:00:00  0.527 
+0

請注意,這實際上會返回TIMESTAMP的不確定值(如果您使用CEILING替換FLOOR時可以進行演示) - 但它可能不會實質性影響結果。 – Strawberry