日期:2014-05-17  浏览次数:20705 次

统计查询表的语句如何写,带重复记录
比如说表如下


      id        学员号(xyh)   考试次数(kscs)    结果(cj) 座位号(zwh)       
      1          A001           1                 不及格    001
      2          A001           2                 及格      001
      3          A002           1                 及格      002
      4          A003           1                 不及格    001
      5          A003           2                  及格     001

如上,就是 这个表中流水号对应的就是考生,每个考生有2次的机会,如果第一次考试及格了,就不用考第二次了,如果说

第一次不及格,还能考第二次,我现在就是要根据这个表做统计,统计出合格人数与不合格人数,就是说即使表中学员有考

两次的,也只以他最后一次考试成绩为准,

这个表中有人考两次,也有人只考了一次,请问这样的语句该如何写才能统计真实的考试合格人数与不合格人数
------最佳解决方案--------------------

select count(distinct case when kscs = 1 and cj = '及格' then xyh
              when kscs = 2 and cj = '及格' then xyh else then null end) as 及格人数,
   count(distinct case when kscs = 2 and cj = '不及格' then xyh else then null end) as 不及格人数,
   count(distinct xyh) as 总人数
from tb_name

------其他解决方案--------------------

select sum(decode(cj,'及格',1,0)) 及格数,sum(decode(cj,'及格',0,1)) 不及格数,count(1) 总数 from (select max(cj) cj from t group by xyh);

------其他解决方案--------------------
select sum(cj) pass,count(1)-sum(cj) delay from
(select sum(decode(cj,'及格',1,0)) cj from tb group by xyh)

------其他解决方案--------------------

select count(distinct case when kscs = 1 and cj = '及格' then xyh
              when kscs = 2 and cj = '及格' then xyh else null end) as 及格人数,
   count(distinct case when kscs = 2 and cj = '不及格' then xyh)&n