用下面的语句进行按月统计
select convert(char(7),mal_time0,102),count(mal_src) from monitor_mal如果没有四月份的记录,则统计结果就没有四月份就没有那一行
这样再用图形显示的时候就会出问题,我想在统计结果中加入四月份那一行,应该怎么做?

解决方案 »

  1.   

    另外建立一个12个月份的表,用left outer jion连接
      

  2.   

    先在monitor_mal选择一个日期最大最小范围,比如2003.01到2003.12
    然后用循环依次以这样的格式插入到临时表temp(month,mcount)中2003.01     0
    2003.02     0
    ...
    2003.12     0然后用楼主的数据来更新临时表的相应月份的数据
      

  3.   

    --我写了一段sql代码,你试试
    declare @maxdate datetime,@mindate datetime
    select @maxdate=max(mal_time0),@mindate=min(mal_time0)  --取日期范围
    from monitor_malcreate table #temp(mon char(7),mcount int)              --日期范围数据初始化
    while (@mindate<=@maxdate)
    begin
      insert into #temp values(convert(char(7),@mindate,102),0)
      set @mindate=dateadd(mm,1,@mindate)
    endselect convert(char(7),mal_time0,102) as mal_mon,count(mal_src) as mal_src
    into #temp1                                             --更新        
    from monitor_mal
    group by convert(char(7),mal_time0,102)update a set a.mcount=b.mal_src
    from #temp a,#temp1 b
    where a.mon=b.mal_monselect * from #temp                                     --最终数据集drop table #temp1
    drop table #temp
      

  4.   

    to jinjazz(我是jin) 
    如果时间跨年好像就不行了to fenght2004(fht)
    这样的方法是可行的,但是对数据库性能的影响太大了吧?