如何使用MySQL DB计算一天中行程次数的分布

时间:2015-09-02 12:22:20

标签: java mysql sql average distribution

我有一个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}

也许我可以使用带AVERAGE的SQL执行相同的任务?

1 个答案:

答案 0 :(得分:1)

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行结果集,而不是数十亿次旅行。

可以使用WHEREGROUP BYORDER BY条款中的各种复杂程度。但这应该让你开始。