MySQL 查询一天中房协访问量最高的时间
MySQL query to find hour of the day when the number of housing society visits is highest
房协到访数据
Id
Contact
Entry_time
Exit_time
Duration of Stay
1
8080808080
26/07/2021 08:00:05
26/07/2021 08:23:06
181
2
9692596925
26/07/2021 08:12:49
26/07/2021 08:14:44
115
3
7099270992
26/07/2021 11:02:49
26/07/2021 11:14:44
715
4
8900289002
26/07/2021 16:12:49
26/07/2021 16:14:44
115
5
9089590895
26/07/2021 15:12:49
26/07/2021 15:14:44
115
6
8765087650
26/07/2021 19:12:49
26/07/2021 19:14:44
115
7
7862178621
26/07/2021 18:12:49
26/07/2021 18:14:44
115
访问数据是多年可用的,可以包含数百万行,因此该解决方案的时间复杂度应该很低。
预期输出:8-9 AM(因为最高访问次数 (2) 是在该小时内进行的)。
当我阅读这个问题时,这将对您有很大帮助。
您可以做几件事来改善这一点:
- 用小时数创建单独的 table 以避免联合(或用数字 table 来做);
- 单独存储时间,TIME() 方法会减慢查询速度。
.
SELECT
hours.`hour`,
COUNT(occurrence.perfId) AS occurrences
FROM (
SELECT '00:00' AS `hour`
UNION SELECT '01:00'
UNION SELECT '02:00'
UNION SELECT '03:00'
UNION SELECT '04:00'
UNION SELECT '05:00'
UNION SELECT '06:00'
UNION SELECT '07:00'
UNION SELECT '08:00'
UNION SELECT '09:00'
UNION SELECT '10:00'
UNION SELECT '11:00'
UNION SELECT '12:00'
UNION SELECT '13:00'
UNION SELECT '14:00'
UNION SELECT '15:00'
UNION SELECT '16:00'
UNION SELECT '17:00'
UNION SELECT '18:00'
UNION SELECT '19:00'
UNION SELECT '20:00'
UNION SELECT '21:00'
UNION SELECT '22:00'
UNION SELECT '23:00'
) hours
LEFT JOIN YOUR_TABLE occurrence ON hours.`hour` BETWEEN TIME(occurrence.Entry_time) AND TIME(occurrence.Exit_time)
GROUP BY hours.`hour`
ORDER BY occurrences DESC
将产生(来自测试数据库的数据):
[hour] [occurrences]
10:00 73554
11:00 67492
09:00 65679
08:00 63886
13:00 63565
12:00 62525
07:00 61500
14:00 53095
15:00 49017
16:00 41955
17:00 31991
18:00 21251
06:00 17591
19:00 13717
20:00 8532
21:00 4421
22:00 2050
23:00 818
05:00 796
04:00 561
01:00 175
03:00 123
02:00 120
00:00 23
要获取小时,只需用查询和 select 第一个包裹:
SELECT hour FROM (
# Paste query from above here...
) hourData
LIMIT 1;
我能够使用以下查询获得所需的输出:
SELECT HOUR(Entry_time) as hr FROM table_name GROUP BY HOUR(Entry_time) ORDER BY COUNT(*) DESC LIMIT 1;
感谢@nicholascarey 的解决方案:SQL to determine peak volume and hour for all days
房协到访数据
Id | Contact | Entry_time | Exit_time | Duration of Stay |
---|---|---|---|---|
1 | 8080808080 | 26/07/2021 08:00:05 | 26/07/2021 08:23:06 | 181 |
2 | 9692596925 | 26/07/2021 08:12:49 | 26/07/2021 08:14:44 | 115 |
3 | 7099270992 | 26/07/2021 11:02:49 | 26/07/2021 11:14:44 | 715 |
4 | 8900289002 | 26/07/2021 16:12:49 | 26/07/2021 16:14:44 | 115 |
5 | 9089590895 | 26/07/2021 15:12:49 | 26/07/2021 15:14:44 | 115 |
6 | 8765087650 | 26/07/2021 19:12:49 | 26/07/2021 19:14:44 | 115 |
7 | 7862178621 | 26/07/2021 18:12:49 | 26/07/2021 18:14:44 | 115 |
访问数据是多年可用的,可以包含数百万行,因此该解决方案的时间复杂度应该很低。
预期输出:8-9 AM(因为最高访问次数 (2) 是在该小时内进行的)。
当我阅读这个问题时,这将对您有很大帮助。 您可以做几件事来改善这一点:
- 用小时数创建单独的 table 以避免联合(或用数字 table 来做);
- 单独存储时间,TIME() 方法会减慢查询速度。
.
SELECT
hours.`hour`,
COUNT(occurrence.perfId) AS occurrences
FROM (
SELECT '00:00' AS `hour`
UNION SELECT '01:00'
UNION SELECT '02:00'
UNION SELECT '03:00'
UNION SELECT '04:00'
UNION SELECT '05:00'
UNION SELECT '06:00'
UNION SELECT '07:00'
UNION SELECT '08:00'
UNION SELECT '09:00'
UNION SELECT '10:00'
UNION SELECT '11:00'
UNION SELECT '12:00'
UNION SELECT '13:00'
UNION SELECT '14:00'
UNION SELECT '15:00'
UNION SELECT '16:00'
UNION SELECT '17:00'
UNION SELECT '18:00'
UNION SELECT '19:00'
UNION SELECT '20:00'
UNION SELECT '21:00'
UNION SELECT '22:00'
UNION SELECT '23:00'
) hours
LEFT JOIN YOUR_TABLE occurrence ON hours.`hour` BETWEEN TIME(occurrence.Entry_time) AND TIME(occurrence.Exit_time)
GROUP BY hours.`hour`
ORDER BY occurrences DESC
将产生(来自测试数据库的数据):
[hour] [occurrences]
10:00 73554
11:00 67492
09:00 65679
08:00 63886
13:00 63565
12:00 62525
07:00 61500
14:00 53095
15:00 49017
16:00 41955
17:00 31991
18:00 21251
06:00 17591
19:00 13717
20:00 8532
21:00 4421
22:00 2050
23:00 818
05:00 796
04:00 561
01:00 175
03:00 123
02:00 120
00:00 23
要获取小时,只需用查询和 select 第一个包裹:
SELECT hour FROM (
# Paste query from above here...
) hourData
LIMIT 1;
我能够使用以下查询获得所需的输出:
SELECT HOUR(Entry_time) as hr FROM table_name GROUP BY HOUR(Entry_time) ORDER BY COUNT(*) DESC LIMIT 1;
感谢@nicholascarey 的解决方案:SQL to determine peak volume and hour for all days