1

This question is related to a post I made earlier: MySQL: Calculating Data from Table with One Month Offset

But now I need to build a procedure that queries a table of contact data stored by week. Here's a simplified example of the table I am working with:

+-----------------+------------+
| week_start_date |  contacts  |
+-----------------+------------+
|    2015-03-08   |     12     |
|    2015-03-01   |     20     |
|    2015-02-22   |      5     |
|    2015-02-15   |     17     |
|    2015-02-08   |      8     |
|    2015-02-01   |      2     |
|    2015-01-25   |     16     |
|    2015-01-18   |     10     |
|    2015-01-11   |      4     |
|       ...       |    ...     |
+-----------------+------------+

What I need to figure out is how to calculate a 4 week moving average that also has a 4 week offset on top of that. For instance, if I wanted to get the average contacts for the week of March 8, 2015, it would be the average of January 18 through February 8. In the example above, my average would be: (10 + 16 + 2 + 8 ) / 4 = 9. And if I wanted to find the average for the week of March 1, 2015, then it would be the average of January 11 through February 1 which comes out to be 8 using the sample table above.

From my last post, I know that I can handle the 4 week offset by joining the table with itself on the week_start_date similar to this:

SELECT s1.week_start_date, s2.Total_Contacts
  FROM sample_table s1
       LEFT JOIN (SELECT week_start_date, sum(contacts) AS Total_Contacts
                    FROM sample_table
                  GROUP BY week_start_date) s2
          ON s1.week_start_date =
                date_add(s2.week_start_date, INTERVAL 4 WEEK)
 WHERE s1.week_start_date = '2015-03-08'
GROUP BY s1.week_start_date;

But getting it to compute the four week average as well is where I am stuck. I thought joining it on a range of dates would work, but I keep getting averages that are a lot larger than expected. I'm guessing it is due to how the week_start_date's are being grouped. (Note that there can be multiple records for each week. I only show one record for each week on the sample table to make it less cluttered.)

Is joining on a date range the correct approach? Or do I need to add another join somewhere?

Thanks for your help.

2 Answers 2

1

I would suggest using a correlated subquery:

select st.*,
       (select avg(contacts)
        from sample_table st2
        where st2.week_start_date >= st.week_start_date - interval 7 * 7 days and
              st2.week_start_date <= st.week_start_date - interval 4 * 7 days
       ) as avg_4week_delayed
from sample_table st;
1
  • Looks perfect to me ;)
    – Maciej Los
    Mar 20, 2015 at 19:23
0

I would use the DATE_SUB() function and just subtract the necessary weeks you need. So, for March 8 in your example, try something like this:

SELECT AVG(contacts)
FROM myTable
WHERE week_start_date <= DATE_SUB('2015-03-08', INTERVAL 4 WEEK) AND week_start_date >= DATE_SUB('2015-03-08', INTERVAL 7 WEEK);

It worked in SQL Fiddle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.