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

  • Home
  • SEARCH
  • 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 7940987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:31:45+00:00 2026-06-03T23:31:45+00:00

How do you create a moving average in SQL? Current table: Date Clicks 2012-05-01

  • 0

How do you create a moving average in SQL?

Current table:

Date             Clicks 
2012-05-01       2,230
2012-05-02       3,150
2012-05-03       5,520
2012-05-04       1,330
2012-05-05       2,260
2012-05-06       3,540
2012-05-07       2,330

Desired table or output:

Date             Clicks    3 day Moving Average
2012-05-01       2,230
2012-05-02       3,150
2012-05-03       5,520          4,360
2012-05-04       1,330          3,330
2012-05-05       2,260          3,120
2012-05-06       3,540          3,320
2012-05-07       2,330          3,010
  • 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-03T23:31:47+00:00Added an answer on June 3, 2026 at 11:31 pm

    One way to do this is to join on the same table a few times.

    select
     (Current.Clicks 
      + isnull(P1.Clicks, 0)
      + isnull(P2.Clicks, 0)
      + isnull(P3.Clicks, 0)) / 4 as MovingAvg3
    from
     MyTable as Current
     left join MyTable as P1 on P1.Date = DateAdd(day, -1, Current.Date)
     left join MyTable as P2 on P2.Date = DateAdd(day, -2, Current.Date)
     left join MyTable as P3 on P3.Date = DateAdd(day, -3, Current.Date)
    

    Adjust the DateAdd component of the ON-Clauses to match whether you want your moving average to be strictly from the past-through-now or days-ago through days-ahead.

    • This works nicely for situations where you need a moving average over only a few data points.
    • This is not an optimal solution for moving averages with more than a few data points.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What's the most efficient way to create a moving average or rolling sum in
Im trying to create a moving back ground. My goal is to have a
I am trying to create a moving ball animation in my app everything is
I want to create a Pong game with a moving ball and all the
I just create a new repo https://github.com/tolbard/ember-moving . In this repo I extend the
In JavaFx i can easy create animations, moving pieces etc., but as far as
I have a problem with moving animation. I want to create animation which start
Before moving to PDO, I created SQL queries in PHP by concatenating strings. If
create table ImagenesUsuario { idImagen int primary key not null IDENTITY } This doesn't
I'm trying to create moving functionality to my imageView (maskPreview in the code below),

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.