关于月份查询的一个小问题,帮忙解决,再线等
select   year(inssj)   +    '- '   +   month(inssj)   ,   count(*)   from   datas 
 group   by   year(inssj)   +    '- '   +   month(inssj) 
 order   by   year(inssj)   +    '- '   +   month(inssj)   
 这样会得到   
 2007-06	10 
 2007-05	300 
 2007-04	400 
 2007-03	420 
 2007-01	510   
 因为我2月没有数据 
 如果才能得到   
 2007-06	10 
 2007-05	300 
 2007-04	400 
 2007-03	420 
 2007-02	0 
 2007-01	510   
 这样一个结果呢?
------解决方案--------------------不怎么好查!
------解决方案--------------------可以参考: 
 http://community.csdn.net/Expert/topic/5560/5560882.xml?temp=.189892
------解决方案--------------------如果知道有多少个月, 则用left/right join 就行了   
 select a.ym, cnt = isnull(b.cnt, 0) 
 from( 
 select ym =  '2007-06 ' union all 
 select ym =  '2007-05 ' union all 
 select ym =  '2007-04 ' union all 
 select ym =  '2007-03 ' union all 
 select ym =  '2007-02 ' union all 
 select ym =  '2007-01 ' 
 )a left join( 
 select ym = year(inssj) +  '- ' + month(inssj) , cnt = count(*)  
 from datas 
 group by year(inssj) +  '- ' + month(inssj) 
 )b on a.ym = b.ym 
 order by a.ym
------解决方案--------------------如果不知道有多少个月, 则你需要用动态sql去生成上面查询中, 子查询a的语句 
 或者用生成一个包含所有月份的临时表
------解决方案--------------------create table #YMTable(TYear int ,TMonth int )                                      
  declare @tmpDate datetime                                       
  select @tmpDate = @startDate                                         
  while(@tmpDate  < @endDate)                   
  begin                   
   set @StartYear=year(@tmpDate)                   
   set @StartMonth =month(@TmpDate)                                       
   insert into #YMTable(TYear,TMonth) values (@StartYear,@StartMonth)                                         
   select @tmpDate = dateAdd(month, 1, @tmpDate)                   
  end  
 注意:    
 @startDate 和 @endDate就是你要找的月份的开始月和结束月的一个日期.在用left join就可以了
------解决方案--------------------先生成一个包含所有月份的临时表,然后左连接就行呀.
------解决方案--------------------支持楼上的
------解决方案--------------------这样可以吗?     
 select convert(varchar(7),inssj,120),count(*) from 表 
 group by convert(varchar(7),inssj,120) 
 order by convert(varchar(7),inssj,120)