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 7663375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:02:54+00:00 2026-05-31T14:02:54+00:00

I want to calculate a 12 month moving average from a MySQL column. The

  • 0

I want to calculate a 12 month moving average from a MySQL column. The data represents time-series power measurements, it is a largish dataset (every 10 minutes for several years). A high performance query would be nice but speed is something I can work out later.

DTE                  Active
2012-1-3 00:10       500
2012-1-3 00:20       520
... etc

The following query gives me the total Active for each month :

SELECT YEAR(DTE) AS year, MONTH(DTE) AS month, SUM(Active)/6 as total FROM saturne s GROUP BY YEAR(DTE), MONTH(DTE)

The following query gives me the moving average Active for a given month and year – say october 2011

SELECT SUM(Active)/6 AS average FROM saturne 
WHERE (YEAR(DTE) = 2011 AND MONTH(DTE) <= 10) OR (YEAR(DTE) = 2010 AND MONTH(DTE) > 10) 

I would however like to generate a query which returns the monthly total and the 12 month moving average in the next column.

year        month        total        average
2012        2            701474       9258089
2012        1            877535       9386664
... etc

(The factor of 6 is because the data represents instantaneous power recorded every 10 minutes, dividing the total by 6 gives the total energy)

  • 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-05-31T14:02:56+00:00Added an answer on May 31, 2026 at 2:02 pm

    Try:

    SELECT YEAR(GDTE) AS year, 
           MONTH(GDTE) AS month, 
           SUM(case when i=0 then Active end)/6 as total,
           SUM(Active)/(MAX(i+1)*6) as avg
    FROM
    (select s.*, DATE_ADD(DTE, INTERVAL m.i MONTH) GDTE, m.i
     FROM saturne s
     cross join (select 0 i union select 1 union select 2 union select 3 union 
                 select 4 union select 5 union select 6 union select 7 union 
                 select 8 union select 9 union select 10 union select 11) m
    ) sq
    WHERE GDTE <= curdate()
    GROUP BY YEAR(GDTE), MONTH(GDTE)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to calculate the total no of weeks in current month. Starting from
I want to calculate the execution time of my ASP.NET pages and display it
I want to calculate the next business date for a given month and date.
I have a table with subscription values and want to calculate the time when
I want to calculate the week number of the month,I want to calculate the
Possible Duplicate: How do I calculate relative time? I want to convert Date Value
I've got a query and I want to add parameters to calculate the moving
I want to calculate the start date of the last month. I have referred
I want to get the number of Registrations back from a time period (say
I have an integer column that holds a Unix epoch time value. I want

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.