mysql图表AVG计算上个月每小时的订单数量

时间:2017-01-02 13:04:35

标签: php mysql charts

我正在尝试一段时间来计算上个月每小时订单平均值的图表。 我找到了一些在工作台上工作的功能,但没有在图表上工作,我需要一些帮助。

  • 每一行都是订单。
  • 时间栏名为" creation_time"。

这是每日平均值的示例。它在工作台上工作但不在图表上工作:

SELECT day_of_week, AVG(order_count) average_order FROM 
(
  SELECT DAYNAME(creation_time) day_of_week, 
         DAYOFWEEK(creation_time) day_num, 
         TO_DAYS(creation_time) date,
         count(*) order_count
  FROM devsendi_dev.orders 
  GROUP BY date
) temp
GROUP BY day_of_week 
ORDER BY day_num

1 个答案:

答案 0 :(得分:0)

30天数据的24小时每小时平均值:

SELECT hour_1, AVG(order_count) average_order FROM 
(
  SELECT 
        date(creation_time) as date_of_month,
        DAYNAME(creation_time) day_of_week, 
        DAYOFWEEK(creation_time) day_num, 

        HOUR(creation_time) hour_1,
        count(*) order_count
  FROM devsendi_dev.orders 
  where creation_time<=now() and creation_time>=date_sub(creation_time, interval 1 month)
  GROUP BY date_of_month,hour_1
) temp
GROUP BY hour_1 
ORDER BY hour_1

30天数据的24小时/工作日的每小时平均值:

SELECT day_of_week,hour_1, AVG(order_count) average_order,day_num FROM 
(
  SELECT 
        date(creation_time) as date_of_month,
        DAYNAME(creation_time) day_of_week, 
        DAYOFWEEK(creation_time) day_num, 

        HOUR(creation_time) hour_1,
        count(*) order_count
  FROM devsendi_dev.orders 
  where creation_time<=now() and creation_time>=date_sub(creation_time, interval 1 month)
  GROUP BY date_of_month,hour_1
) temp
GROUP BY day_of_week,hour_1  
ORDER BY day_num