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

依然是分组打分的问题
员工 评分 打分人
1 合格 33
2 合格 22
2 不合格 44
2 合格 33
3 不合格 22
4 合格 11


现在打分人有5位,11 22 33 44 55,是作为表头固定的
我需要得到比如
员工 合格数 不合格数 打分人11 打分人22 打分人33 打分人44 打分人55

------解决方案--------------------
SQL code

with t as(
select 1 yg,'合格' pf,'33' dfr from dual
union all
select 2,'合格','22' from dual
union all
select 2,'合格','44' from dual
union all
select 2,'合格','33' from dual
union all
select 3,'合格','22' from dual
union all
select 4,'合格','11' from dual
)
select yg 员工,
       sum(decode(pf, '合格', 1, 0)) 合格数,
       sum(decode(pf, '不合格', 1, 0)) 不合格数,
       sum(decode(dfr, '11', 1, 0)) 打分人11, 
       sum(decode(dfr, '22', 1, 0)) 打分人22, 
       sum(decode(dfr, '33', 1, 0)) 打分人33, 
       sum(decode(dfr, '44', 1, 0)) 打分人44, 
       sum(decode(dfr, '55', 1, 0)) 打分人55 
from t group by yg


 
        员工        合格数       不合格数      打分人11      打分人22      打分人33      打分人44      打分人55
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
         1          1          0          0          0          1          0          0
         2          3          0          0          1          1          1          0
         3          1          0          0          1          0          0          0
         4          1          0          1          0          0          0          0

------解决方案--------------------
SQL code

create table t1 (y_no number,score varchar2(10),d_no number);

insert into t1 values (1,'合格',33);
insert into t1 values (1,'不合格',55);
insert into t1 values (2,'合格',22);
insert into t1 values (2,'不合格',44);
insert into t1 values (2,'合格',33);
insert into t1 values (3,'不合格',22);
insert into t1 values (4,'合格',11);


select t1.y_no 员工,
       nvl(sum(decode(score,'合格',1,0)),0) 合格数,
       nvl(sum(decode(score,'不合格',1,0)),0) 不合格数,
       decode(sum(case when score='合格' and d_no=11 then 1 when  score='不合格' and d_no=11 then 0 end),1,'合格',0,'不合格') 打分人11,
       decode(sum(case when score='合格' and d_no=22 then 1 when  score='不合格' and d_no=22 then 0 end),1,'合格',0,'不合格') 打分人22,
       decode(sum(case when score='合格' and d_no=33 then 1 when  score='不合格' and d_no=33 then 0 end),1,'合格',0,'不合格') 打分人33,
       decode(sum(case when score='合格' and d_no=44 then 1 when  score='不合格' and d_no=44 then 0 end),1,'合格',0,'不合格') 打分人44,
       decode(sum(case when score='合格' and d_no=55 then 1 when  score='不合格' and d_no=55 then 0 end),1,'合格',0,'不合格') 打分人55
from t1
group by t1.y_no
order by t1.y_no

员工 合格数 不合格数 打分人11  打分人22  打分人33  打分人44  打分人55
--------------------------------------
1    1    1                合格             不合格
2    2    1        合格      合格       不合格    
3    0    1        不合格            
4    1    0    合格