1
我有一個表,看起來像這樣:如何選擇以15分鐘間隔分組的最大列「y」選擇的列「x」的平均值?
CREATE TABLE `visits` (
`idsite` int(10) unsigned NOT NULL,
`idvisit` int(10) unsigned NOT NULL,
`server_time` datetime NOT NULL,
`actions` float DEFAULT NULL,
)
我想要得到的actions
平均值爲15分鐘,其中不同idvisit
之和最大的時間間隔。
我開始這樣做:
select max(counted), server_time
from (select count(distinct idvisit) as counted, server_time
from visits group by year(server_time), month(server_time), day(server_time), (4 * HOUR(server_time) + FLOOR(MINUTE(server_time)/15))) as counts;
我得到了最大的考察,但錯誤server_time
的。我不知道如何獲得有關孔的行,以便在正確的區間中選擇動作的平均值。
樣本數據:
+--------+---------+--------------+---------------------+
| idsite | idvisit | actions | server_time |
+--------+---------+--------------+---------------------+
| 1 | 1 | 14 | 2015-09-15 22:06:57 |
| 1 | 2 | 60 | 2015-09-16 22:09:41 |
| 1 | 3 | 5 | 2015-09-16 22:09:54 |
| 1 | 3 | 40 | 2015-09-16 22:16:58 |
| 1 | 4 | 6 | 2015-09-16 22:19:04 |
| 1 | 5 | 7 | 2015-09-16 22:40:53 |
| 1 | 6 | 1 | 2015-09-16 23:01:41 |
| 1 | 6 | 5 | 2015-09-16 23:08:54 |
| 1 | 7 | 10 | 2015-09-16 23:19:58 |
| 1 | 8 | 6 | 2015-09-16 23:26:14 |
| 1 | 9 | 7 | 2015-09-16 23:48:53 |
+--------+---------+--------------+---------------------+
預期的結果:
+--------------+---------------------+
| AVG(actions) | DATE(server_time) |
+--------------+---------------------+
| 26.33 | 2015-09-15 |
+--------------+---------------------+
其對應於第15分鐘,其中有一個最大不同idvisit的是。
有人可以幫忙嗎?
顯示一些示例數據,比如十幾行或更少的數據預期的結果,並討論任何棘手的邊緣條件。一個sqlfiddle會很好,所以我們不需要做點修改 – Drew
- 基於樣本數據,你如何獲得'26.33'和'2015-09-15'?我認爲'2015-09-16 22:09:41'和'2015-09-19 22:19:04'之間的間隔是最大的獨特身份,所以平均值是'(60 + 5 + 40 + 6 )/ 4 = 27.75',對吧? – Aguardientico
忘了提及時間間隔應該用:00,:15,:30,:45 – mouuu