表1 考勤表:职工号 姓名 考勤项目 年份 月份 表2 考勤项目表:迟到 早退 旷工 请假 
---
select 职工号,
       姓名,
       迟到次数 = sum(case when 考勤项目 = '迟到' then 1 else 0 end),
       早退次数 = sum(case when 考勤项目 = '早退' then 1 else 0 end),
       旷工次数 = sum(case when 考勤项目 = '旷工' then 1 else 0 end),
       请假次数 = sum(case when 考勤项目 = '请假' then 1 else 0 end),
       月份
from Ta
group by 职工号,
         姓名,
         月份
       

解决方案 »

  1.   


    --假设表2的字段名为:考勤项目select a.职工号,a.姓名,
      sum(case b.考勤项目 when '迟到' then 1 else 0 end) '迟到次数',
      sum(case b.考勤项目 when '早退' then 1 else 0 end) '早退次数',
      sum(case b.考勤项目 when '旷工' then 1 else 0 end) '旷工次数',
      sum(case b.考勤项目 when '请假' then 1 else 0 end) '请假次数',
    a.年份,a.月份
    from 表1 a , 表2 b
    where a.考勤项目 = b.考勤项目
    group by a.职工号,a.姓名,a.年份,a.月份
      

  2.   

    insert into table3
    select 职工号,
           姓名,
           迟到次数 = sum(case when 考勤项目 = '迟到' then 1 else 0 end),
           早退次数 = sum(case when 考勤项目 = '早退' then 1 else 0 end),
           旷工次数 = sum(case when 考勤项目 = '旷工' then 1 else 0 end),
           请假次数 = sum(case when 考勤项目 = '请假' then 1 else 0 end),
           月份
    from Ta
    group by 职工号,
             姓名,
             月份
      

  3.   

    insert into table3 
    select 职工号, 
           姓名, 
           迟到次数 = sum(case when 考勤项目 = '迟到' then 1 else 0 end), 
           早退次数 = sum(case when 考勤项目 = '早退' then 1 else 0 end), 
           旷工次数 = sum(case when 考勤项目 = '旷工' then 1 else 0 end), 
           请假次数 = sum(case when 考勤项目 = '请假' then 1 else 0 end), 
           月份 
    from Ta 
    group by 职工号, 
             姓名, 
             月份 
      

  4.   

    我换了,where a.考勤项目 = b.考勤项目
    只是假设表2就一个字段.
      

  5.   

    create table 考勤表(职工号 int,姓名 varchar(10),考勤项目 int, 年份 int,月份 int) 
    create table 考勤项目表(id int,xm varchar(10))
    create table 考勤统计表(职工号 int, 姓名 varchar(10), 迟到次数  int,早退次数 int,旷工次数 int, 请假次数 int, 月份 int)
    insert  考勤项目表 select 1,'迟到' 
    insert  考勤项目表 select 2,'早退' 
    insert  考勤项目表 select 3,'旷工' 
    insert  考勤项目表 select 4,'请假' 
    insert 考勤表 select 1,'1',1,2008,1
    insert 考勤表 select 1,'1',2,2008,1
    insert 考勤表 select 1,'1',3,2008,1
    insert 考勤表 select 1,'1',4,2008,1
    insert 考勤表 select 2,'2',1,2008,1
    gocreate procedure sf_形成考勤统计表 
    @m int
    as
    delete from 考勤统计表--删除已有数据
    if exists (select * from 考勤表 where 月份 = @m)
    begin
    --从考勤表导入
    insert into 考勤统计表
    (职工号,姓名,迟到次数,早退次数,旷工次数,请假次数,月份)select 考勤表.职工号,考勤表.姓名,
      sum(case 考勤项目表.xm when '迟到' then 1 else 0 end) '迟到次数',
      sum(case 考勤项目表.xm when '早退' then 1 else 0 end) '早退次数',
      sum(case 考勤项目表.xm when '旷工' then 1 else 0 end) '旷工次数',
      sum(case 考勤项目表.xm when '请假' then 1 else 0 end) '请假次数',
      月份
    from 考勤表, 考勤项目表
    where 考勤表.考勤项目 = 考勤项目表.id 
    group by 考勤表.职工号,考勤表.姓名,考勤表.月份
    end
    go
    exec sf_形成考勤统计表 1select * from 考勤统计表drop proc sf_形成考勤统计表drop table 考勤表,考勤项目表,考勤统计表
    /*职工号         姓名         迟到次数        早退次数        旷工次数        请假次数        月份          
    ----------- ---------- ----------- ----------- ----------- ----------- ----------- 
    1           1          1           1           1           1           1
    2           2          1           0           0           0           1(所影响的行数为 2 行)*/