我想计算相对于它们在数据库中写入的月份的行数。我的数据库有一个名为created_date
的列。您可以在下面看到我做了什么:
select month(created_date) as "Month",count(created_date) as "Count" from transactions group by month(created_date)
此查询返回的内容是这样的:
{'Month':1,'Count':10}
之所以会这样,是因为我的数据库中只有一个月,而我需要的结果是所有月份,包括数据库中不存在的月份,就像这样:
{'Month':1,'Count':10}
{'Month':2,'Count':0}
{'Month':3,'Count':0}
{'Month':4,'Count':0}
{'Month':5,'Count':0}
{'Month':6,'Count':0}
{'Month':7,'Count':0}
{'Month':8,'Count':0}
{'Month':9,'Count':0}
{'Month':10,'Count':0}
{'Month':11,'Count':0}
{'Month':12,'Count':0}
我应该怎么做?