1

我有一个表格,用于存储不同考试的学生成绩和不同的考试类型,比如主要考试、持续评估、课程作业等,我需要查询该表格,以便我只获得一个特定考试单元的一行,平均百分比取决于学生参加的考试次数。这是我尝试的查询:

select stu_reg_no, unit_code, 
       exam_unit, exam_semester, 
       student_year,  
       sum(per_centage_score)/count(per_centage_score) percentage
 from student_results_master
group by unit_code, exam_unit, 
         per_centage_score, stu_reg_no,  
         exam_semester, student_year;

这是我的结果集:

在此处输入图像描述

对于同一个考试单元,我有两行,因为一个是主要考试,而另一个课程作业我需要这样的输出:

E35/1000/2013   TFT001  COMPLEX ANALYSIS   1  1  71.04
E35/1000/2013   TFT002  LINEAR ALGEBRA     1  1  56.25

该特定单元的百分比被添加并除以该特定单元的考试数量。我怎样才能做到这一点?

4

2 回答 2

2

Oracle 提供了一个内置函数,用于计算一组行上的表达式的平均值 - AVG()。要获得所需的输出,您需要执行以下两件事:

  1. 替换sum(per_centage_score)/count(per_centage_score)avg(per_centage_score)
  2. 从子句中删除per_centage_score列。group by

为此,您的查询可能如下所示:

select stu_reg_no
     , unit_code
     , exam_unit
     , exam_semester
     , student_year
     , avg(percentage) percentage
 from student_results_master
group by unit_code
       , exam_unit
       , stu_reg_no
       , exam_semester
       , student_year;

结果:

STU_REG_NO    UNIT_CODE EXAM_UNIT        EXAM_SEMESTER STUDENT_YEAR PERCENTAGE
------------- --------- ---------------- ------------- ------------ ----------
E35/1000/2013 TFT001    COMPLEX ANALYSIS             1            1      71.04 
E35/1000/2013 TFT002    LINEAR ALGEBRA               1            1      56.25
于 2013-10-10T08:24:41.773 回答
0

尝试这个:

select stu_reg_no, unit_code, exam_unit, exam_semester, student_year,  
(select sum(per_centage_score) from student_results_master t2 where t2.exam_unit = t1.exam_unit)
/(select count(per_centage_score) from student_results_master t2 where t2.exam_unit = t1.exam_unit)
from student_results_master t1
group by unit_code, exam_unit, stu_reg_no,  exam_semester, student_year;
于 2013-10-10T08:26:03.457 回答