数据库记录了1-12月份每个员工的加班情况,
    记录表的结构:加班记录id  --加班员工id  --加班时间
      现在想得到一份统计列表,
前台页面结构如下,
   月份  实加班  应加班  存休  
  1月份            4     ?天  
  2月份            4     ?天    
  3月份            4     ?天    
  4月份            4     ?天  
  5月份            4     ?天
  6月份            4     ?天
  7月份            4     ?天
  8月份            4     ?天 
  9月份            4     ?天 
 10月份            4     ?天 
 11月份            4     ?天 
 12月份            4     ?天  求一个比较好的方式,得到员工1-12月份的实加班情况统计。

解决方案 »

  1.   

    需要一个日期字段date吧
    select 加班员工id,sum(加班时间) from table group by 加班员工id,datepart(month,date)
    这样得到员工每个月的加班时间 
      

  2.   

    按月分组统计
    select 
      '数量' as ' ',
      sum(case month(日期) when 1 then 数量 else 0 end) as [一月],
      sum(case month(日期) when 2 then 数量 else 0 end) as [二月],
      sum(case month(日期) when 3 then 数量 else 0 end) as [三月],
      sum(case month(日期) when 4 then 数量 else 0 end) as [四月]
    from tb
    http://topic.csdn.net/u/20080829/13/3d1eabe0-c558-4f8c-a373-b060af66e2eb.html
      

  3.   


    --by Month
    declare @time1 datetime, @time2 datetime
    set @time1='2009-11-14' 
    set @time2='2009-11-14'
    select convert(varchar(7),k.rn+@time1-1,120) as Date ,isnull(sum(GetMoney),0) as GetMoney
    from  AgentAmountLog a right join 
    (select ROW_NUMBER() over(order by getdate()) as rn from sys.columns a ,sys.columns b) k
    on DATEDIFF(DAY,a.LogTime,k.rn+@time1-1)=0
    where k.rn+@time1-1<=@time2
    group by convert(varchar(7),k.rn+@time1-1,120)
    order by Date 
      

  4.   

    declare @table table(加班员工id varchar(10),加班时间 datetime)insert into @table values('1','2009-01-01')
    insert into @table values('1','2009-01-02')
    insert into @table values('1','2009-02-01')
    insert into @table values('1','2009-03-01')
    insert into @table values('1','2009-04-01')
    insert into @table values('1','2009-05-01')
    insert into @table values('1','2009-06-01')
    insert into @table values('1','2009-07-01')
    insert into @table values('1','2009-08-01')
    insert into @table values('1','2009-09-01')
    insert into @table values('1','2009-10-01')
    insert into @table values('1','2009-11-01')
    insert into @table values('1','2009-12-01')
    insert into @table values('2','2009-12-01')select 
    case month(加班时间) when 1 then '一月'
    when 2 then '二月' 
    when 3 then '三月' 
    when 4 then '四月' 
    when 5 then '五月' 
    when 6 then '六月' 
    when 7 then '七月' 
    when 8 then '八月' 
    when 9 then '九月' 
    when 10 then '十月' 
    when 11 then '十一月' 
    when 12 then '十二月' end as 月份,
    sum(1) as 加班次数
    from @table
    where 加班员工id='1'
    group by month(加班时间)
      

  5.   

    你的数据已有,那么就按你想显示的格式显示每月情况详表
    最后表格一列显示累计就可以了
    这个显示1使用模板2我觉得直接后台输出更方便更直接些
    http://blog.csdn.net/xianfajushi/archive/2009/07/22/4371397.aspx
      

  6.   

    一个sql语句就可以解决的问题需要什么解决思路?
    难道你还打算使用临时表来储存你的统计数据加快检索时间?
      

  7.   

    楼主需求不明确..   你的加班时间字段是要表达什么信息?
      是加班发生在哪一天?还是加班了几个小时或者是几天?如果是加班时间是datetime类型(加班发生在哪一天)   那6楼完全正解 。
    如果加班时间是数值类型的   那一楼的正解 ,也就是说肯定要有时间字段 ,