I have a table with a date and value column. The table is populated daily with new values. On occasion the population does not take place. I need to generate a report of the latest value on each past Monday. This query shows that no data was collected on 2013-02-18.
mysql> select date from table where dayofweek(date) = 2;
+------------+
| date |
+------------+
| 2013-02-04 |
| 2013-02-11 |
| 2013-02-25 |
| 2013-03-04 |
| 2013-03-11 |
| 2013-03-18 |
| 2013-03-25 |
+------------+
7 rows in set (0.00 sec)
So I'd like to get the latest previous value for that Monday. I can do that with <= max(date) separately if I know the missing date like this:
mysql> select max(date) from table where date <= "2013-02-18";
+------------+
| max(date) |
+------------+
| 2013-02-14 |
+------------+
1 row in set (0.00)
This shows that the latest value for 2013-02-18 was collected on 2013-02-14. So I need a single query that will return what the latest value was on each Monday. And if no value exists for that day use the latest prior existing value. My output should look like this:
+------------+
| date |
+------------|
| 2013-02-04 |
| 2013-02-11 |
| 2013-02-14 |
| 2013-02-25 |
| 2013-03-04 |
| 2013-03-11 |
| 2013-03-18 |
| 2013-03-25 |
+------------+
Thanks in advance.