I have a SQL table:
CREATE TABLE test (
`id` int(11) NOT NULL AUTO_INCREMENT,
`date` date NULL,
`distance` INT(10) DEFAULT NULL,
PRIMARY KEY (`id`)
);
INSERT INTO test (date, distance) VALUES ('2020-01-01','1');
INSERT INTO test (date, distance) VALUES ('2020-01-15','3');
INSERT INTO test (date, distance) VALUES ('2020-02-10','1');
INSERT INTO test (date, distance) VALUES ('2020-02-20','4');
INSERT INTO test (date, distance) VALUES ('2020-05-06','8');
INSERT INTO test (date, distance) VALUES ('2020-11-12','2');
INSERT INTO test (date, distance) VALUES ('2020-11-13','5');
And what I want to do is to have a sum for every month from this year, but what is important to keep months with sum of zero even if I do not have data for this month
Here is the fiddle: https://www.db-fiddle.com/f/9KmGk6uxBQxSRxr5Qsid3S/2
I have tried something like:
SELECT YEAR_MONTH(`date`) as date, SUM(distance) FROM test GROUP BY `date`;