这sql怎么写?????求大神

select month(createtime) month ,count(distinct insId) num
from aa
where convert(varchar(4),createtime,120)= year(getdate())
group by month(createtime)

                                这是sql ,我想查询12个月的数据,但现在只有 11月份有数据,我想列出其他月份,并且num 是0 

能不能 把 结果集 贴张图

图片说明

实例请看!

建个虚拟表 left join

elect month(createtime) month ,count(distinct insId) num
from aa
where convert(varchar(4),createtime,120)= year(getdate())
group by month(createtime)

首先、按照您的思路走的话,最好的办法就是建立一个 中间表 month_temp,储存12个月份的数据,然后用month_temp (左链接) left join 您的表,就能满足您的需求。
然后、下面说下本人的理解:
这种数据,一般情况下都是放在程序里去补全,这样,不至于影响数据库,代码处理也会很快的反馈出结果;再有,sql中最好避免使用函数,尤其是数据量比较大的数据,这种都是可以放到程序里执行或者判断的,这个属于个人的想法,希望会对你有帮助。