简体   繁体   English

如何计算 MySQL 中每天的平均计数?

[英]How to calculate average of count per day in MySQL?

I have a table called SDON, and a query that counts the total number of pallets on each date of import.我有一个名为 SDON 的表,以及一个计算每个进口日期的托盘总数的查询。 I wish to group these together and average the counts per month.我希望将这些组合在一起并平均每月的计数。

My count query:我的计数查询:

SELECT COUNT(`storage unit`) AS `Pallets`, `import_date` 
FROM `SDON` 
GROUP BY `import_date`

查询结果

I wish for the following:我希望有以下几点:

平均托盘 |进口月份 18500

You can group the results of your original query by year and month of the date:您可以按日期的年份和月份对原始查询的结果进行分组:

SELECT EXTRACT(YEAR_MONTH FROM import_date) AS import_month
     , AVG(day_total) AS average_per_day
FROM (
    SELECT import_date
         , COUNT(`storage unit`) AS day_total
    FROM sdon
    GROUP BY import_date
) AS x
GROUP BY EXTRACT(YEAR_MONTH FROM import_date)

Converting a number such as 202207 to 2022-07 is trivial.将诸如202207之类的数字转换为2022-07是微不足道的。

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

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