Dengan asumsi bahwa tanggal Anda adalah datetime
yang sebenarnya kolom:
SELECT MONTH(date), YEAR(date), id_publisher, COUNT(*)
FROM raw_occurrence_record
GROUP BY MONTH(date), YEAR(date), id_publisher
Anda dapat menggabungkan bulan &tahun Anda seperti ini:
SELECT CONCAT(MONTH(date), '/', YEAR(date)) AS Month, id_publisher, COUNT(*)
FROM raw_occurrence_record
GROUP BY MONTH(date), YEAR(date), id_publisher
Untuk menemukan bulan di mana tidak ada catatan, Anda memerlukan tabel tanggal. Jika Anda tidak dapat membuatnya, Anda dapat UNION ALL
tabel kalender seperti ini:
SELECT a.year, a.month, b.id_publisher, COUNT(b.id_publisher) AS num
FROM
(SELECT 11 AS month, 2012 AS year
UNION ALL
SELECT 12, 2012
UNION ALL
SELECT 1, 2013
UNION ALL
SELECT 2, 2013) a
LEFT JOIN raw_occurence_record b
ON YEAR(b.date) = a.year AND MONTH(b.date) = a.month
GROUP BY a.year, a.month, b.id_publisher