我有两个表 一个是考勤表,一个是考勤统计表
考勤表有列:职员编号 姓名 是否按时出勤 是否早退 是否迟到 是否请假 是否加班 是否矿工 
 
考勤统计表 有职员编号 姓名 按时出勤次数 迟到次数 早退次数 矿工次数 请假次数 加班次数 
 
我要把考勤表中每个字段'是'的次数 统计起来,在考勤统计表中显示 
请问怎么写这个SQL语句

解决方案 »

  1.   

    case when 是否按时出勤 = '是' then 1 else 0 end
      

  2.   

    唉,又是用中文,如果你用1/0那多简单——直接sum
      

  3.   

    sum(case 是否按时出勤 when '是' then 1 else 0 end)其它自己照着写group by 职员编号
      

  4.   

    select
       职员编号,
       姓名,
       sum(case when 是否按时出勤 ='是' then 1 end),
       sum(case when 是否早退='是' then 1 end),
       sum(case when 是否迟到='是' then 1 end),
       sum(case when 是否请假='是' then 1 end),
       sum(case when 是否加班='是' then 1 end),
       sum(case when 是否矿工='是' then 1 end)
    from ta
    group by 职员编号,   姓名
      

  5.   

    insert into 考勤统计表 
    select 职员编号,姓名,
      count(case 是否按时出勤 when 是 then 1 else 0 end),
      count(case 是否迟到 when 是 then 1 else 0 end),
      count(case 是否早退 when 是 then 1 else 0 end),
      count(case 是否旷工 when 是 then 1 else 0 end),
      count(case 是否请假 when 是 then 1 else 0 end),
      count(case 是否加班 when 是 then 1 else 0 end)
    from 考勤表 group by 职员编号,姓名
      

  6.   

    select 职员编号,姓名, 
      count(case 是否按时出勤 when 是 then 1 else 0 end), 
      count(case 是否迟到 when 是 then 1 else 0 end), 
      count(case 是否早退 when 是 then 1 else 0 end), 
      count(case 是否旷工 when 是 then 1 else 0 end), 
      count(case 是否请假 when 是 then 1 else 0 end), 
      count(case 是否加班 when 是 then 1 else 0 end) 
      into 考勤统计表 from 考勤表  group by 职员编号,姓名
      

  7.   

    查询出来很简单,像这样
    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),
       sum(case when 是否加班='是' then 1 else 0 end),
       sum(case when 是否矿工='是' then 1 else 0 end)
    from ta
    group by 职员编号,   姓名
    --
    但是你要在考勤统计表中显示,是什么意思,这是个统计表,得按时间段来统计,这样的话应该从考勤表中去出时间段内的记录,然后在分组,最后插入到统计表中。
      

  8.   

    select 职员编号, 姓名,
    sum(出勤) as 按时出勤次数,sum(迟到) as 迟到次数,sum(早退) as 早退次数,
    sum(矿工) as 矿工次数,sum(请假) as 请假次数,sum(矿工) as 加班次数
    from
    (
       select 
       职员编号, 姓名, 
       case when 是否按时出勤 ='是' then 1 else 0 end as 出勤, 
       case when 是否早退='是' then 1 else 0 end as 早退, 
       case when 是否迟到='是' then 1 else 0 end as 迟到, 
       case when 是否请假='是' then 1 else 0 end 请假, 
       case when 是否加班='是' then 1 else 0 end 加班, 
       case when 是否矿工='是' then 1 else 0 end 矿工 
       from ta 
    ) as tab
    group by 职员编号, 姓名
      

  9.   

    select a.职员编号,a.姓名,sum(按时出勤次)as 按时出勤总数 ,sum(迟到次数) as 迟到总次数,sum(矿工次数) AS 矿工总次数,sum(请假次数)as 请假总次数, SUM(加班次数)as 加班总次数
    from 考勤表有列 a JOIN 考勤统计表 b on a.职员编号 = b.有职员编号
    where 是否按时出勤 ='是' and 是否早退= '是' and 是否请假='是' and 是否加班='是' and 是否矿工 ='是'
    group by a.职员编号,a.姓名