我有表名:table1  字段有:id,待处理,已处理我要对这个表每个月进行一次的统计,统计出总共事项数,待处理的统计数,已处理的统计数请问这样的SQL怎么写

解决方案 »

  1.   

    declare @tb1 table(id int,nodetail int,detailed int,date datetime)
    insert into @tb1
    select 1,2,2,'2010-9-10' union all
    select 2,2,2,'2010-9-11' union all
    select 3,2,2,'2010-9-12' union all
    select 4,2,2,'2010-9-13' union all
    select 5,2,2,'2010-9-14' union all
    select 6,2,2,'2010-9-15' union all
    select 7,2,2,'2010-10-10' union all
    select 8,2,2,'2010-10-11' union all
    select 9,2,2,'2010-10-12' union all
    select 10,2,2,'2010-10-13' union all
    select 11,2,2,'2010-11-14' union all
    select 12,2,2,'2010-11-15'
    select [month]=month(date),alldetail=sum(nodetail+detailed),nodetail=sum(nodetail),detailed=sum(detailed) from @tb1
    group by month(date)
    /*
    9 24 12 12
    10 16 8 8
    11 8 4 4
    */
      

  2.   


    select count(id) as allnum,
    sum(case when Note=1 then 1 else 0 end) waitnum,
    sum(case when Note=3 then 1 else 0 end) donenum
    from Administrative
    如果我这样写有没有问题,我就想让它自动安装系统时间去划分
      

  3.   

    楼主是想每个月的固定时间执行一次,你可以存储过程,然后在SQLServer里面建立一个任务,然后让任务在每隔固时间自动执行。
      

  4.   

    http://www.chinaz.com/Program/MSSQL/122A64A2008_2.html创建计划任务
      

  5.   

    Sorry,说错了,是在SqlServer代理里面建立一个作业
      

  6.   

    晕·你是想说一个月自动执行一次吧?你这需求没说明白哦~~
    去baidu或者GG搜 sql server job
      

  7.   

    1 count(*)
    2 count(待处理的统计数)
    3 count(*)-count(待处理的统计数)