I'd like to split number of emails added to the database by month and year.
My query:
SELECT TOP 200 Monthh, Yearr, COUNT(Email) AS Amount
FROM
(SELECT Email, MONTH(Added_date) AS Monthh, YEAR(Added_date) AS Yearr
FROM Contacts) a
GROUP BY Monthh, Yearr
ORDER BY CAST(Yearr AS INT), CAST(Monthh AS INT)
But let's say that my (very simplified) contact list looks like this:
| Email | Added_date |
| --------------- | -------------- |
| max@gmail.com | 2021-10-01 |
| emma@gmail.com | 2021-10-05 |
| tony@gmail.com | 2021-12-06 |
| mark@gmail.com | 2022-01-23 |
| lucy@gmail.com | 2022-01-28 |
| chris@gmail.com | 2022-02-04 |
In this case, the result would look like this:
| Yearr | Monthh | Amount |
| ----- | ------ | ------ |
| 2021 | 10 | 2 |
| 2021 | 12 | 1 |
| 2022 | 01 | 2 |
| 2022 | 02 | 1 |
This works. However, there's a slight issue - November 2021 is not returned (which is of course highly logical), but I'd like to return it anyway in my result with null (0, zero etc.) value.
My dream result would be this:
| Yearr | Monthh | Amount |
| ----- | ------ | ------ |
| 2021 | 10 | 2 |
| 2021 | 11 | 0 |
| 2021 | 12 | 1 |
| 2022 | 01 | 2 |
| 2022 | 02 | 1 |
I can't seem to find an easy solution. And you can easily that the real split needs to be done back to year 2000.
Btw, I am running this query in SOQL and BigQuery if it's important.
Hope it's all clear and thank you for your help!