I'm following the excellent solution from Guido concerning date queries where a given date may not be present in the database https://stackoverflow.com/a/36378448/6591500.
I've got the solution working as is without any issues. I now need expand with this to include a WHERE clause. I have a 'date' column and a 'results' column, I need to count the number of 'passes' on each given day in the results column.
My SQL is this:
SELECT report_date, COUNT(*)-1 AS the_tally FROM (
SELECT date(report_date) as report_date FROM data as t1
WHERE 'spf_result' = 'pass'
UNION ALL
SELECT curdate() - interval a day AS report_date FROM (
select 1 as a 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 union
select 12 union select 13 union select 14
) as t2
) as t3 GROUP BY report_date ORDER BY report_date DESC LIMIT 14
The result I'm expecting is:
report_date The_tally
2020-06-11 4
2020-06-10 4
2020-06-09 6
2020-06-08 4
The result I'm getting is:
report_date The_tally
2020-06-11 0
2020-06-10 0
2020-06-09 0
2020-06-08 0
Example data is:
report_date spf_result
2020-06-11 pass
2020-06-11 pass
2020-06-11 pass
2020-06-11 pass
2020-06-10 pass
2020-06-10 pass
2020-06-10 pass
2020-06-10 pass
2020-06-09 pass
etc...
Why am I getting zeros?