Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8282181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:17:06+00:00 2026-06-08T10:17:06+00:00

I have a table something like this. count | date —————— 1 2012-01-01 4

  • 0

I have a table something like this.

    
    count | date
    ------------------
     1     2012-01-01   
     4     2012-01-01   
     5     2012-01-02
    12     2012-01-03
     7     2012-01-04
     4     2012-01-05
    19     2012-01-06
     1     2012-01-07
     etc...

I’m looking for a way to calculate the average count per week over the previous 4 week period for each week.

The results should be something like…

    
    avg | yearweek
    ------------------
     3     201201   
     5     201202   
     6     201203
     1     201204
     11    201205
     3     201206
    18     201207
    12     201208
     etc...

…where each yearweek is the weekly average over the past 4 yearweeks.

Getting the weekly averages is simple enough but how do I then get that over the past 4 yearweeks? And then how to do I do that as a rolling average? Am I better off just doing this in code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T10:17:07+00:00Added an answer on June 8, 2026 at 10:17 am

    While you could certainly do this in the code of your application, if you really need to do it in SQL, you could first create a table of results aggregated by week and then join it to itself to get the 4-week moving average.

    In doing so, instead of storing the averages, I would store the sums and the number of days (1st or last week of year might not have 7 days – thinking of the edge cases). That way, you would avoid calculating unweighted averages when the denominators of averages are different.

    So let’s say you have a table “weekly_results”, which has fields: yearweek, sumcount, numdays. You can now self-join to the last 4 weeks and get the sums and counts, and then calculate the averages from that:

    SELECT yearweek, sum_cnt/sum_dys as avg_moving_4wk
    FROM (
        SELECT a.yearweek, sum(b.sumcount) as sum_cnt, sum(b.numdays) as sum_dys
        FROM weekly_results a
            join weekly_results b
            on a.yearweek - b.yearweek <4 and a.yearweek - b.yearweek >=0
        GROUP BY a.yearweek
    ) t1
    GROUP BY yearweek
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table like this in my DB Date ItemId Count
I have a table that looks something like this: id | firstperson | secondperson
I have a table that looks something like this: Name Year Value A 2000
I have a relationships table, the table looks something like this ------------------------ | client_id
I have a MySQL table that looks something like this: +-----+------------+ | id |
I currently have a table structure that looks something like this(some details omitted): ColumnName
Let's say I have a MySQL table that is something like this: software table:
I have a table named size here, something like this: size id | size
I have a question. My database table is something like this: | ID |
I have something like this: create table account ( id int identity(1,1) primary key,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.