考勤记录表:
员工ID     考勤时间
0000001    2005-12-01 09:30:00
0000001    2005-12-02 09:31:00输入参数: 上班时间 例如 09:30:00 改如何生成当月或者指定月的考勤统计表表?员工ID  1日  2日  3日  4日…………
0000001 按时 迟到 按时 按时
-----------------
呵呵忘记写完整了。。根据输入参数中的上班时间来判断啊

解决方案 »

  1.   

    declare @l_month int --考勤月份select 表1.员工ID  1日  2日  3日  4日…………31日
    from
    (select 员工ID, case when 考勤时间<=上班时间 then '按时' else '迟到' end as 1日 
        from 考勤记录表 where month(考勤时间)=@l_month and day(考勤时间)=1 ) as 表1
    full join (select 员工ID,case when 考勤时间<=上班时间 then '按时' else '迟到' end 
        as 2日 from 考勤记录表 where month(考勤时间)=@l_month and day(考勤时间)=2 ) as 表2
    on 表1.员工ID=表2.员工ID
    full join (select 员工ID, case when 考勤时间<=上班时间 then '按时' else '迟到' end 
        as 3日 from 考勤记录表 where month(考勤时间)=@l_month and day(考勤时间)=3 ) as 表3
    on 表1.员工ID=表3.员工ID
    ......
    full join (select 员工ID, case when 考勤时间<=上班时间 then '按时' else '迟到' end 
       as 31日 from 考勤记录表 where month(考勤时间)=@l_month and day(考勤时间)=31) as 表31
    on 表1.员工ID=表31.员工ID
    至于本月有30日还是31日不用我说了吧,你自己也会的对吧。我这里假定以31日计算了。
      

  2.   

    大概就是这样了
    create procedure test(@year int, @month int, @time varchar(8))--时间格式hh:mm:ss
    as
    begin
      if object_id('tempdb..#test') is not null drop table #test
      select 员工ID, day(考勤时间) as 日期,
         (case when 考勤时间 <= cast(convert(varchar(10), 考勤时间, 121) + ' ' + @time) as datetime) then '按时' else '迟到' end) as 考勤
      into #test
      from 勤记录表
      where year(考勤时间) = @year and month(考勤时间) = @month
      declare @s varchar(8000)
      set @s = 'select 员工ID'
      select @s = @s + ', min(case when 日期 = '+ cast(日期 as varchar(2)) + ' then 考勤 end) as ' + cast(日期 as varchar(2)) + '日'
      from (select distinct 日期 from #test) aa
      set @s = @s + ' from #test group by 员工ID'
      exec(@s)
      drop table #test
    end
    -----
    可能有问题,我没空测试了,说说思路,楼主自己去测试和修改吧
    首先生成一个临时表记录员工某月的考勤,如下:
    员工ID   日期  考勤
    00001     1     按时
    00002     1     按时
    00001     2     按时
    00002     2     迟到
    然后来一个表90度转换,这种方法的实现在CSDN上有很多
      

  3.   

    行变列
    http://community.csdn.net/Expert/topic/4324/4324734.xml?temp=.5537836
      

  4.   

    改好了,测试成功。原来有两个小错误:多了一个反括号,还有就是新字段名不规范,要加[]
    if object_id('勤记录表') is not null drop table 勤记录表
    go
    select '001' as 员工ID, '2005-11-01 08:29:00' as 考勤时间
    into 勤记录表
    union select '002', '2005-11-01 08:26:00'
    union select '003', '2005-11-01 08:23:00'
    union select '004', '2005-11-01 08:28:00'
    union select '001', '2005-11-02 08:33:00'
    union select '002', '2005-11-02 08:23:00'
    union select '003', '2005-11-02 08:30:00'
    union select '004', '2005-11-02 08:24:00'
    union select '001', '2005-11-03 08:30:00'
    union select '001', '2005-11-04 08:32:00'
    go
    if object_id('test') is not null drop procedure test
    go
    create procedure test(@year int, @month int, @time varchar(8))--时间格式hh:mm:ss
    as
    begin
      if object_id('tempdb..#test') is not null drop table #test
      select 员工ID, day(考勤时间) as 日期,
         (case when 考勤时间 <= cast(convert(varchar(10), 考勤时间, 121) + ' ' + @time as datetime) then '按时' else '迟到' end) as 考勤
      into #test
      from 勤记录表
      where year(考勤时间) = @year and month(考勤时间) = @month
      declare @s varchar(8000)
      set @s = 'select 员工ID'
      select @s = @s + ', min(case when 日期 = '+ cast(日期 as varchar(2)) + ' then 考勤 end) as [' + cast(日期 as varchar(2)) + '日]'
      from (select distinct 日期 from #test) aa
      set @s = @s + ' from #test group by 员工ID'
      exec(@s)
      drop table #test
    end
    go
    select * from 勤记录表
    /*
    员工ID  考勤时间
    001 2005-11-01 08:29:00
    001 2005-11-02 08:33:00
    001 2005-11-03 08:30:00
    001 2005-11-04 08:32:00
    002 2005-11-01 08:26:00
    002 2005-11-02 08:23:00
    003 2005-11-01 08:23:00
    003 2005-11-02 08:30:00
    004 2005-11-01 08:28:00
    004 2005-11-02 08:24:00
    */
    exec test 2005, 11, '08:30:00'
    /*
    员工ID   1日     2日     3日     4日
    001 按时 迟到 按时 迟到
    002 按时 按时 NULL NULL
    003 按时 按时 NULL NULL
    004 按时 按时 NULL NULL
    */
    go
    drop procedure test
    drop table 勤记录表
      

  5.   

    跪谢 dulei115......除了给分我还能做什么呢。
    等我测试通过后结帖