weekly moving average from current_date to the last 52 weeks I'm a new head to the mysql programming. I have to write a procedure for calculating a weekly moving average grouped by week from 'monday-friday'.

Criteria is : current_date is the end date. start date should be either previous 52 weeks if available or if the number of weeks is less then the moving average should be calculated till the available date.
SELECT g(f(date)) AS 'week of',
       AVG(...) AS 'moving average'
    FROM tbl
    WHERE date &gt; CURDATE() - INTERVAL 52 WEEK
    GROUP BY f(date)


Where f() is a function that turns a date (or datetime or timestamp) into a number.  Something like FLOOR((TO_DAYS(date) - 3) / 7), where the 3 is chosen based on what day a week "starts" on.  (I doubt if '3' is correct.)  g() is the inverse:  FROM_DAYS((x * 7) + 3).  Test:

mysql&gt; SELECT                            NOW(), 
                          FLOOR((TO_DAYS(now()) - 3) / 7) AS 'f(now)',
              FROM_DAYS( (FLOOR((TO_DAYS(now()) - 3) / 7) * 7) + 3) AS 'g(f(now))';
+---------------------+--------+------------+
| NOW()               | f(now) | g(f(now))  |
+---------------------+--------+------------+
| 2016-05-24 17:13:54 | 105210 | 2016-05-24 |
+---------------------+--------+------------+