当前位置: 代码迷 >> Oracle开发 >> 依然是分组打分的有关问题
  详细解决方案

依然是分组打分的有关问题

热度:42   发布时间:2016-04-24 07:31:58.0
依然是分组打分的问题
员工 评分 打分人
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 dualunion allselect 2,'合格','22' from dualunion allselect 2,'合格','44' from dualunion allselect 2,'合格','33' from dualunion allselect 3,'合格','22' from dualunion allselect 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,'不合格') 打分人55from t1group by t1.y_noorder by t1.y_no员工 合格数 不合格数 打分人11  打分人22  打分人33  打分人44  打分人55--------------------------------------1    1    1                合格             不合格2    2    1        合格      合格       不合格    3    0    1        不合格            4    1    0    合格
  相关解决方案