select 人员,
sum(工作量) as 工作量总和,
sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数
from 表
group by 人员

解决方案 »

  1.   

    楼上的漏掉了查询时间段的条件,加个
    select 人员,
    sum(工作量) as 工作量总和,
    sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
    sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
    sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
    sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数
    from 表
    where 工作时间 between 开始时间 and 结束时间
    group by 人员
      

  2.   

    人员:谁(可以不要,不要就是全部人员的)
    对于这个结果: 可以用union 连接一个总的统计结果集,即select 人员,
    sum(工作量) as 工作量总和,
    sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
    sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
    sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
    sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数
    from 表
    where 工作时间 between 开始时间 and 结束时间
    group by 人员unionselect null,sum(a.工作量总和),sum(a.及时记录数),sum(a.不及时记录数),
    sum(a.满意记录数),sum(a.不满意记录数) 
    from
    (select 人员,
    sum(工作量) as 工作量总和,
    sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
    sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
    sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
    sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数
    from 表
    where 工作时间 between 开始时间 and 结束时间
    group by 人员)a
      

  3.   

    比如指定哪个时间段的统计
    -------------------------
    加条件时就象这样啊: where 工作时间 between '2003/01/05' and '2005/09/02'
      

  4.   

    如果:要得到及时率怎么统计?
    及时率=个人及时数/个人总数*%也结合到SQL中
      

  5.   

    及时率=个人及时数/个人总数*%
    ----------------------------
    直接写的话就是,加条语句:
    cast(及时记录数/(及时记录数+不及时记录数)*100 as varchar) +'%' as 及时率 
    就得到50.25%这样的形式!!!但要注意了, 由于及时记录数和不及时记录数都是整数,2/4 的结果是0 而2/4.0 的结果是 0.5
    所以要改写一下:
    cast(及时记录数/(及时记录数+不及时记录数+0.0)*100 as varchar) +'%' as 及时率 
      

  6.   

    变通一下就可以了阿!!!
    select 人员,
    sum(工作量) as 工作量总和,
    sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
    sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
    sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
    sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数,
    sum(case 是否及时 when '是' then 1 else 0 end)/sum(工作量) as 及时率
    from 表
    where 工作时间 between 开始时间 and 结束时间
    group by 人员
      

  7.   

    declare @a table(人员 varchar(8),工作量 int,是否及时 char(2),是否满意 char(2),工作时间 datetime)insert @a 
    select '小王','5','是','是','2005-01-05' union all
    select '小李','5','是','否','2005-09-02' union all
    select '小毛','4','否','是','2005-01-05' union all
    select '小蓝','9','否','否','2005-08-05' union all
    select '小王','1','否','是','2005-01-05' select  
    人员,
    sum(工作量)as 工作量总和  ,
    sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
    sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
    sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
    sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数
    from @a
    group by 人员
    -------------------------------
    小蓝 9 0 1 0 1
    小李 5 1 0 0 1
    小毛 4 0 1 1 0
    小王 6 1 1 2 0
      

  8.   

    select 人员,
    sum(工作量) as 工作量总和,
    sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
    sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
    sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
    sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数,
    sum(case 是否及时 when '是' then 1 else 0 end)/sum(工作量) as 及时率
    from 表
    where 工作时间 between 开始时间 and 结束时间
    group by 人员应该是正确的
      

  9.   

    declare @t table(人员 char(10),工作量 int,是否及时 char(10),是否满意 char(10),工作时间 datetime)
    insert @t 
    select '小王',5,'是','是','2005/01/05' union all
    select '小李',5,'是','否','2005/09/02' union all
    select '小毛',4,'否','是','2003/01/05' union all
    select '小蓝',9,'否','否','2005/08/05' union all
    select '小王',1,'否','是','2005/01/05'select 人员
    ,工作量总和=sum(工作量)
    ,及时记录数=sum(case 是否及时 when '是' then 1 when '否' then 0 end)
    ,不及时记录数=sum(case 是否及时 when '否' then 1 when '是' then 0 end)
    ,满意记录数=sum(case 是否满意 when '是' then 1  when '否' then 0 end)
    ,不满意记录数=sum(case 是否满意 when '否' then 1 when '是' then 0 end )
    from @t
    group by 人员/*
    人员   工作量总和      及时记录数  不及时记录数  满意记录数  不满意记录数  
    小蓝       9 0 1 0 1
    小李       5 1 0 0 1
    小毛       4 0 1 1 0
    小王       6 1 1 2 0
    */
      

  10.   

    declare @t table(人员 char(10),工作量 int,是否及时 char(10),是否满意 char(10),工作时间 datetime)
    insert @t 
    select '小王',5,'是','是','2005/01/05' union all
    select '小李',5,'是','否','2005/09/02' union all
    select '小毛',4,'否','是','2003/01/05' union all
    select '小蓝',9,'否','否','2005/08/05' union all
    select '小王',1,'否','是','2005/01/05'select 人员
    ,工作量总和=sum(工作量)
    ,及时记录数=sum(case 是否及时 when '是' then 1 when '否' then 0 end)
    ,不及时记录数=sum(case 是否及时 when '否' then 1 when '是' then 0 end)
    ,满意记录数=sum(case 是否满意 when '是' then 1  when '否' then 0 end)
    ,不满意记录数=sum(case 是否满意 when '否' then 1 when '是' then 0 end )
    from @t
    where 工作时间 between '2003/01/05' and '2005/09/02'
    group by 人员
    /*
    人员   工作量总和      及时记录数  不及时记录数  满意记录数  不满意记录数  
    小蓝       9 0 1 0 1
    小李       5 1 0 0 1
    小毛       4 0 1 1 0
    小王       6 1 1 2 0
    */
      

  11.   

    --测试数据
    create table gz
    (ry nvarchar(50),
     gzl int,
     ji nvarchar(10),
     my nvarchar(10),
     sj datetime)
    go
    insert gz
    select '小王',5,'是','是','2005/01/05' union all
    select '小李',5,'是','否','2005/09/02' union all
    select '小毛',4,'否','是','2003/01/05' union all
    select '小蓝',9,'否','否','2005/08/05' union all
    select '小王',1,'否','是','2005/01/05'
    go
    --查询语句
    select  ry as 人员,
    工作两总和  =sum(case gzl when  gzl then gzl else '' end),
    及时记录数  =sum(case ji  when '是' then  1  else '' end),
    不及时记录数=sum(case ji  when '否' then  1  else '' end),
    满意记录数  =sum(case my  when '是' then  1  else '' end),
    不满意记录数=sum(case my  when '否' then  1  else '' end)
    from gz 
    group by ry 
    -----result------
    /*
    人员   工作量总和      及时记录数  不及时记录数  满意记录数  不满意记录数  
    小蓝       9 0 1 0 1
    小李       5 1 0 0 1
    小毛       4 0 1 1 0
    小王       6 1 1 2 0
    */
      

  12.   

    大家好踊跃哦,顶一下,工作量排序在后边加个 order by 工作量总和 就可以了
      

  13.   

    select 人员,
    sum(工作量) as 工作量总和,
    sum(case 是否及时 when '是' then 1 else 0 end) as 及时记录数,
    sum(case 是否及时 when '否' then 1 else 0 end) as 不及时记录数,
    sum(case 是否满意 when '是' then 1 else 0 end) as 满意记录数,
    sum(case 是否满意 when '否' then 1 else 0 end) as 不满意记录数,
    sum(case 是否及时 when '是' then 1 else 0 end)/sum(工作量) as 及时率
    from 表
    where 工作时间 between 开始时间 and 结束时间
    group by 人员后面加ORDER BY 好象是不可以的.
      

  14.   

    路过
    真是佩服SQL版兄弟们的热情和好耐心
    说实话,感觉楼主够懒,好象一点脑筋都不想动。这样能学到东西?
      

  15.   


    --查询语句
    select ry as 人员,
    工作两总和  =sum(case gzl when  gzl then gzl else '' end),
    及时记录数  =sum(case ji  when '是' then  1  else '' end),
    不及时记录数=sum(case ji  when '否' then  1  else '' end),
    满意记录数  =sum(case my  when '是' then  1  else '' end),
    不满意记录数=sum(case my  when '否' then  1  else '' end)
    from gz 
    group by ry
    order by 工作两总和 desc
    可以啊 怎么不可以order by 呢??????
    楼主努力啊  不能总靠兄弟门帮忙啊