我有一张桌子可以存储不同考试的学生成绩,不同的考试类型说主要考试,连续评估,课程作业等等,我需要查询表格,这样我只能获得一个特定考试单元的一行平均取决于学生参加的考试数量。这是我尝试过的查询:
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
添加该特定单位的百分比,并除以该特定单位的考试数。 我怎样才能做到这一点?
答案 0 :(得分:2)
Oracle提供了一个内置函数,用于计算一组行上的表达式的平均值 - AVG()。要获得所需的输出,您需要执行以下两项操作:
sum(per_centage_score)/count(per_centage_score)
替换为avg(per_centage_score)
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
答案 1 :(得分: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;