I built a query to allow me to return the average rating of some applications.
But for some of them, we donβt have data for a month, because the application is new (let's say the application is missing from this month, and therefore we collect data from this month)
SELECT DATE_FORMAT(date, '%Y-%m'), app_id, AVG(rank) FROM wadstats.applestore_ranking where app_id IN (100, 2, 3, 4, 5, 6) GROUP BY MONTH(date), app_id ORDER BY CASE WHEN app_id = 100 THEN 1 ELSE 2 END, date ASC
I needed to show app_id = 100 first
But for app_id = 8, I have no data for August.
Then the results look like
'2015-07', '100', '3.9355' '2015-04', '100', '49.5000' '2015-08', '100', '5.2258' '2015-05', '100', '16.3333' '2015-09', '100', '6.1333' '2015-06', '100', '7.5667' '2015-10', '100', '5.7727' '2015-04', '2', '6.0000' '2015-08', '2', '9.8710' '2015-05', '2', '6.4667' '2015-09', '2', '8.9667' '2015-06', '2', '8.5333' '2015-10', '2', '9.9545' '2015-07', '2', '10.5806' '2015-05', '3', '56.3929' '2015-09', '3', '55.1667' '2015-06', '3', '35.2500' '2015-07', '3', '38.7143' '2015-04', '3', '38.7500' '2015-08', '3', '52.5500' '2015-09', '4', '30.2105' '2015-06', '4', '27.9231' '2015-10', '4', '30.0000' '2015-07', '4', '47.0000' '2015-08', '4', '32.6818' '2015-06', '5', '46.8667' '2015-10', '5', '86.6667' '2015-07', '5', '63.5185' '2015-04', '5', '24.2500' '2015-08', '5', '67.3571' '2015-10', '6', '30.1818'
Instead, I want to have zero for every month, even if there was no data for that month
Expected results
'2015-07', '100', '3.9355' '2015-04', '100', '49.5000' '2015-08', '100', '5.2258' '2015-05', '100', '16.3333' '2015-09', '100', '6.1333' '2015-06', '100', '7.5667' '2015-10', '100', '5.7727' '2015-04', '2', '6.0000' '2015-08', '2', '9.8710' '2015-05', '2', '6.4667' '2015-09', '2', '8.9667' '2015-06', '2', '8.5333' '2015-10', '2', '9.9545' '2015-07', '2', '10.5806' '2015-05', '3', '56.3929' '2015-09', '3', '55.1667' '2015-06', '3', '35.2500' '2015-07', '3', '38.7143' '2015-04', '3', '38.7500' '2015-08', '3', '52.5500' '2015-09', '4', '30.2105' '2015-06', '4', '27.9231' '2015-05', '4', NULL '2015-10', '4', '30.0000' '2015-07', '4', '47.0000' '2015-08', '4', '32.6818' '2015-06', '5', '46.8667' '2015-10', '5', '86.6667' '2015-07', '5', '63.5185' '2015-04', '5', '24.2500' '2015-08', '5', '67.3571' '2015-04', '6', NULL '2015-05', '6', NULL '2015-06', '6', NULL '2015-07', '6', NULL '2015-08', '6', NULL '2015-09', '6', NULL '2015-10', '6', '30.1818'
If I need to have 0 instead of NULL, that will be fine too, but I need every month in the DB to have a value for each app_id
Thank you very much in advance