简体   繁体   English

MySQL AVG(COUNT(*) - 订单按星期查询?

[英]MySQL AVG(COUNT(*) - Orders By day of week query?

This query has baffled me... I've searched the web work over a day now and I have tried numerous things. 这个问题令我感到困惑......我已经搜索了一天的网络工作,我尝试了很多东西。

I want to get the avg number of orders for every day of the week from my db. 我希望从我的数据库获得每周每天的平均订单数量。 I can pull the total # with COUNT just fine. 我可以用COUNT来计算总数#就好了。 But I just can't figure out how to get the AVG of COUNT on a GROUP BY. 但我无法弄清楚如何在GROUP BY上获得COUNT的AVG。 I've tried subqueries... functions... everything... nothing works... maybe someone can throw me a bone. 我已经尝试了子查询......功能......一切......没有用......也许有人可以给我一个骨头。

Here is the query I started with below. 这是我在下面开始的查询。 I know AVG(COUNT(*)) won't work but I'll leave it at that because it shows what I want to do. 我知道AVG(COUNT(*))不起作用,但我会留下它,因为它显示了我想做的事情。

SELECT 
    AVG(COUNT(*)) AS avgorders, 
    SUM(total) AS ordertotal, 
    DAYNAME(STR_TO_DATE(order_time,'%m/%d/%Y %H:%i')) AS day 
FROM data 
GROUP BY day 
ORDER BY DAYOFWEEK(STR_TO_DATE(order_time,'%m/%d/%Y %H:%i')) ASC

To get the average you don't need the grand totals for each day, you need multiple daily totals for each day. 要获得平均值,您不需要每天的总计,您需要每天多个每日总计。

  Day    |  Count
__________________
 Monday        5
 Tuesday       4
 Monday        6
 Tuesday       3
 ...          ...

Then you can average those numbers. 然后你可以平均这些数字。 Ie (5+6)/2 for Monday. 即星期一(5 + 6)/ 2。
Something like this should work: 这样的事情应该有效:

SELECT day_of_week, AVG(order_count) average_order FROM 
(
  SELECT DAYNAME(order_date) day_of_week, 
         DAYOFWEEK(order_date) day_num, 
         TO_DAYS(order_date) date,
         count(*) order_count
  FROM data 
  GROUP BY date
) temp
GROUP BY day_of_week 
ORDER BY day_num

UPDATE: I was originally wrong. 更新:我本来是错的。 Group the inner SELECT by the actual date to get the correct daily totals. 将内部SELECT按实际日期分组以获得正确的每日总计。 For instance, you need to get how many orders happened Monday (2/1/10) and Monday (2/8/10) separately. 例如,您需要分别在星期一(2/1/10)和星期一(2/8/10)获得多少订单。 Then average those totals by the day of the week. 然后将这些总数按星期几计算。

This will do, assuming that order_time is date or datetime field ( everyone would be hapier this way ;) ). 这样做,假设order_timedatedatetime字段(每个人都会这样做;))。 Of course there is some approximation, because oldest order can be in Friday and newest in Monday, so amount of every day of week isn't equal, but creating separate variable for every day of week will be pain in the ass. 当然有一些近似值,因为最老的订单可以在周五和周一的最新订单,因此每周的每一天的数量不相等,但是为每周的每一天创建单独的变量将是痛苦的屁股。 Anyway I hope it will be helpful for now. 无论如何,我希望它现在会有所帮助。

SET @total_weeks = (
    SELECT
        TIMESTAMPDIFF(
            WEEK,
            MIN(order_time),
            MAX(order_time)
        )
     FROM data
    );

SELECT
    DAYNAME(order_time) AS day_of_week,
    ( COUNT(*) / @total_weeks ) AS avgorders,
    COUNT(*) AS total_orders
FROM 
    data
GROUP BY
    DAYOFWEEK(order_time)

I know this is old, but i was searching for a similar solution hoping to find something someone else had used. 我知道这是旧的,但我正在寻找一个类似的解决方案,希望找到别人用过的东西。 In hopes of not doing a sub query, i came up with the below and would love any feed back! 为了不做一个子查询,我提出了下面的内容,并希望任何反馈!

SELECT dayofweek(`timestamp`) as 'Day',count(`OrderID`)/count(DISTINCT day(`timestamp`)) as 'Average' FROM  `Data` GROUP BY dayofweek(`timestamp`)

The idea is to divide the total orders on a given day of the week, by the total number of "Mondays" or whatever day it is. 我们的想法是将一周中某一天的总订单除以“星期一”的总数或任何一天。 What this does not account for would be any days that had zero orders would not lower the average. 这没有考虑到的是零订单的任何日子都不会降低平均值。 That may or may not be desired depending on the application. 根据应用,这可能是也可能不是。

What you are asking doesn't make sense to me... AVG is an aggregate function and so is COUNT. 你问的问题对我来说没有意义...... AVG是一个聚合函数,因此是COUNT。 What's wrong with the query above but just use: COUNT(*) AS avgorders? 上面的查询有什么问题,但只使用:COUNT(*)AS avgorders?

Lets say you had 3 rows for day1, 2 rows for day2, 5 rows for day3, and 9 rows for day4... do you want to get back a single row result that tells you: 假设你说day1有3行,day2有2行,day3有5行,day4有9行...你想要回到单行结果告诉你:

avgorders   = (3 + 2 + 2 + 5 + 9) / 5 = 21 / 5 = 4.2
ordertotal  = (3 + 2 + 2 + 5 + 9)              = 21

I don't think you can get that in a single query, and you'd be better off doing the second round of aggregation in a server side language like PHP operating on the results of the first aggregation. 我不认为你可以在一个查询中得到它,并且你最好在服务于第一次聚合结果的服务器端语言(如PHP)中进行第二轮聚合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM