如何使用 MySQL 数据库计算一天内出行次数的分布
How to calculate the distribution of the number of trips over a day using MySQL DB
我有一个数据库,其中包含一年中不同旅行的历史数据。我需要知道一天中出行次数的分布(在 Java 中)。例如:
00:00-01:00: 1, 01:00-02:00: 0, ..., 23:00-00:00: 4
注意:输出应为 JSON 格式。
我对解决此任务的最佳方法(步骤)很感兴趣。目前我打算通过以下方式解决:
1) Create List<Hashtable<String,Integer>> (Hashtable has 24 keys, each corresponding to one hour interval over the day).
2) Connect to DB and run SQL query in order to download all trips.
3) Run through ResultSet and add 1 to the corresponding day and time slot in List<Hashtable<String,Integer>>
4) Close connection with DB
5) Create Hashtable<String,Integer> with 24 keys, each corresponding to one hour interval over the day.
6) Run over List<Hashtable<String,Integer>>. Calculate an average number of trips per each hourly interval over all days and save results in Hashtable<String,Integer>.
7) Convert Hashtable<String,Integer> to JSON as follows:
示例 JSON 输出:
{"00:00-01:00": 1, "01:00-02:00": 0, ..., "23:00-00:00": 4}
也许我可以使用 SQL 和 AVERAGE 来完成同样的任务?
SQL 聚合查询非常适合这个。
此查询为您提供去年数据库中显示的天数。
SELECT COUNT(DISTINCT(DATE(triptime)))
FROM trip_hour
WHERE YEAR(triptime) = YEAR(NOW()-1)
这会为您提供每个小时时段的行程数。
SELECT HOUR(a.triptime) AS trip_hour,
COUNT(*) AS trip_count
FROM trip
WHERE YEAR(triptime) = YEAR(NOW()-1)
GROUP BY HOUR(triptime)
ORDER BY HOUR(triptime)
最后,这给出了每个小时时段内每天的平均行程数,结合了上述两个查询。
SELECT HOUR(a.triptime) AS trip_hour,
COUNT(*) AS trip_count,
COUNT(*) / (SELECT COUNT(DISTINCT(DATE(triptime)))
FROM trip_hour
WHERE YEAR(triptime) = YEAR(NOW()-1)) AS trip_avg
FROM trip
WHERE YEAR(triptime) = YEAR(NOW()-1)
GROUP BY HOUR(triptime)
ORDER BY HOUR(triptime)
这将为您提供一个 24 行的结果集供您下载,而不是大量的行程。
可以使用 WHERE
、GROUP BY
和 ORDER BY
子句中的各种复杂方法。但这应该可以帮助您入门。
我有一个数据库,其中包含一年中不同旅行的历史数据。我需要知道一天中出行次数的分布(在 Java 中)。例如:
00:00-01:00: 1, 01:00-02:00: 0, ..., 23:00-00:00: 4
注意:输出应为 JSON 格式。
我对解决此任务的最佳方法(步骤)很感兴趣。目前我打算通过以下方式解决:
1) Create List<Hashtable<String,Integer>> (Hashtable has 24 keys, each corresponding to one hour interval over the day).
2) Connect to DB and run SQL query in order to download all trips.
3) Run through ResultSet and add 1 to the corresponding day and time slot in List<Hashtable<String,Integer>>
4) Close connection with DB
5) Create Hashtable<String,Integer> with 24 keys, each corresponding to one hour interval over the day.
6) Run over List<Hashtable<String,Integer>>. Calculate an average number of trips per each hourly interval over all days and save results in Hashtable<String,Integer>.
7) Convert Hashtable<String,Integer> to JSON as follows:
示例 JSON 输出:
{"00:00-01:00": 1, "01:00-02:00": 0, ..., "23:00-00:00": 4}
也许我可以使用 SQL 和 AVERAGE 来完成同样的任务?
SQL 聚合查询非常适合这个。
此查询为您提供去年数据库中显示的天数。
SELECT COUNT(DISTINCT(DATE(triptime)))
FROM trip_hour
WHERE YEAR(triptime) = YEAR(NOW()-1)
这会为您提供每个小时时段的行程数。
SELECT HOUR(a.triptime) AS trip_hour,
COUNT(*) AS trip_count
FROM trip
WHERE YEAR(triptime) = YEAR(NOW()-1)
GROUP BY HOUR(triptime)
ORDER BY HOUR(triptime)
最后,这给出了每个小时时段内每天的平均行程数,结合了上述两个查询。
SELECT HOUR(a.triptime) AS trip_hour,
COUNT(*) AS trip_count,
COUNT(*) / (SELECT COUNT(DISTINCT(DATE(triptime)))
FROM trip_hour
WHERE YEAR(triptime) = YEAR(NOW()-1)) AS trip_avg
FROM trip
WHERE YEAR(triptime) = YEAR(NOW()-1)
GROUP BY HOUR(triptime)
ORDER BY HOUR(triptime)
这将为您提供一个 24 行的结果集供您下载,而不是大量的行程。
可以使用 WHERE
、GROUP BY
和 ORDER BY
子句中的各种复杂方法。但这应该可以帮助您入门。