每月所有周的 SQL 平均值

Posted

技术标签:

【中文标题】每月所有周的 SQL 平均值【英文标题】:SQL average of all weeks by month 【发布时间】:2018-12-14 15:40:59 【问题描述】:

我正在尝试获取每个月所有周的平均值。我怎样才能在 mysql 中做到这一点?以下是我拥有的,也是我需要的:

SELECT                      
  date_part(year, date_of_act) AS CALENDAR_YEAR,                        
  date_part(week, date_of_act) AS CALENDAR_WEEK_OF_YEAR,                        
  (num_active_divs / total_num_divs) AS "Weekly average for each year of week"                      
FROM                        
  Infotable                     
GROUP BY calendar_year, calendar_week_of_year                       
ORDER BY calendar_year, calendar_week_of_year



What I have:        What I have:    What I have:            

Calendar_Year   Days of the year    Calendar_Week_of_year   Weekly average for each year of week            

2018,   Jan 01 - Jan 07,    1,  0.266855            
2018,   Jan 08 - Jan 14,    2,  0.297223            
2018,   Jan 15 - Jan 21,    3,  0.308583            
2018,   Jan 22 - Jan 28,    4,  0.309994            
2018,   Jan 29 - Feb 04,    5,  0.317419            
2018,   Feb 05 - Feb 11,    6,  0.316454            
2018,   Feb 12 - Feb 18,    7,  0.313929            
2018,   Feb 19 - Feb 25,    8,  0.315489            
2018,   Feb 26 - Mar 04,    9,  0.3218          
2018,   Mar 05 - Mar 11,    10, 0.308509            
2018,   Mar 12 - Mar 18,    11, 0.302866            
2018,   Mar 19 - Mar 25,    12, 0.31586         

What I need:                What I need:    What I need:    What I need:

Calendar_Year       Calendar_Week_of_year   Weekly average for each year of week    Calendar_Month_of_year  Week_of_month   Average for all weeks in that month

2018,   Jan 01 - Jan 07,    1,  0.266855,   1,  1   
2018,   Jan 08 - Jan 14,    2,  0.297223,   1,  2   
2018,   Jan 15 - Jan 21,    3,  0.308583,   1,  3   
2018,   Jan 22 - Jan 28,    4,  0.309994,   1,  4, (0.266855+0.297223+0.308583+0.309994)/4

2018,   Jan 29 - Feb 04,    5,  0.317419,   2,  1   
2018,   Feb 05 - Feb 11,    6,  0.316454,   2,  2   
2018,   Feb 12 - Feb 18,    7,  0.313929,   2,  3   
2018,   Feb 19 - Feb 25,    8,  0.315489,   2,  4,  (0.317419+0.316454+0.313929+0.315489)/4

2018,   Feb 26 - Mar 04,    9,  0.32180,    3,  1   
2018,   Mar 05 - Mar 11,    10, 0.308509,   3,  2   
2018,   Mar 12 - Mar 18,    11, 0.302866,   3,  3   
2018,   Mar 19 - Mar 25,    12, 0.31586,    3,  4,  (0.3218+0.308509+0.302866+0.31586)/4

【问题讨论】:

【参考方案1】:

这里的一个选项是加入一个查找月平均值的子查询:

SELECT
    t1.*,
    t2.avg_weekly_average
FROM yourTable t1
INNER JOIN
(
    SELECT Calendar_Year, (Calendar_Week_of_year - 1) / 4 AS month,
        AVG(weekly_average) AS avg_weekly_average
    FROM yourTable
    GROUP BYCalendar_Year, (Calendar_Week_of_year - 1) / 4
) t2
    ON t1.Calendar_Year = t2.Calendar_Year AND
       (t1.Calendar_Week_of_year - 1) / 4 = t2.month;

如果您使用的是 MySQL 8+,那么您可以在这里使用AVG 作为分析函数。在这种情况下,查询变得不那么简洁了:

SELECT *,
    AVG(weekly_average) OVER (PARTITION BY Calendar_Year,
        (Calendar_Week_of_year - 1) / 4) avg_weekly_average
FROM yourTable t1;

【讨论】:

您好,感谢您的回复。我尝试了“AVG”选项,但没有按预期工作。请参阅下面的输出,它与预期值(最后一列)不同,出于某种原因 - 可能需要以某种方式编辑 select 语句? Calendar_Year Calendar_Week_of_year Weekly average for each year of week Average for all weeks in that month Expected value 2018 1 0.266637 0.204148429 0.29542225 2018 2 0.29698 0.23631892 0.29542225 2018 3 0.308331 0.238056842 0.29542225 2018 4 0.309741 0.246393239 0.29542225 2018 5 0.31716 0.259385717 0.315565 2018 6 0.316196 0.259694425 0.315565 2018 7 0.313673 0.261424683 0.315565 2018 8 0.315231 0.260448818 0.315565 2018 9 0.321537 0.262954257 0.31200375 2018 10 0.308257 0.263811621 0.31200375 2018 11 0.302619 0.25810876 0.31200375 2018 12 0.315602 0.264338671 0.312003 Calendar_Year Calendar_Week_of_year每周平均每年周平均的对所有周在该月的预期值2018 1 0.266637 0.204148429 0.29542225 2018 2 0.29698 0.23631892 0.29542225 2018 3 0.308331 0.238056842 0.29542225 2018 4 0.309741 0.246393239 0.29542225 2018 5 0.31716 0.259385717 0.315565 2018 6 0.316196 0.315565 0.259694425 2018 7 0.313673 0.315565 0.261424683 2018 8 0.315231 0.260448818 0.315565 2018 9 0.321537 0.262954257 0.31200375 2018 10 0.308257 0.263811621 0.31200375 2018 11 0.302619 0.25810876 0.31200375 12 2018 0.315602 0.264338671 0.31200375 跨度>

以上是关于每月所有周的 SQL 平均值的主要内容,如果未能解决你的问题,请参考以下文章

SQL:当某些月份没有记录时,如何查询每月总和的平均值?

SQL:时间维度和计算平均值

使用 R 估计 NetCDF 数据的每月气候学

SQL语句优化-关于分组求平均值

SQL查询以获取时间范围列表的平均值

在 SQL 中使用 Over 和 Partition