2013-10-10 53 views
1

我有一張表,用於存儲不同考試的學生結果以及不同的考試類型,例如主考試,連續評估,課程工作等,我需要查詢表格以便我只獲得一行對於一個特定的考試單元,其平均百分比取決於學生考試的考試數量。這裏是我的嘗試查詢:在Oracle中選擇多行爲一行

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; 

這裏是我的結果集:

enter image description here

我有兩行相同的考試單元,因爲一個是主要的考試,其他課程的工作,我需要我的輸出像這樣:

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

針對該特定單元的百分比加入並通過檢查的數量爲該特定單位分割。 我該如何做到這一點?

回答

2

Oracle提供了一個內置函數,用於計算一組表達式在一組行上的平均值 - AVG()。爲了得到所需的輸出,你需要做以下兩件事情:

  1. 更換sum(per_centage_score)/count(per_centage_score)avg(per_centage_score)
  2. group by條款刪除per_centage_score列。

爲此,您的查詢可能是這樣的:

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 
+0

嗯,我認爲這看起來像它。這對我的工作很好,謝謝。 – ErrorNotFoundException

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; 
+0

你的行仍然是四個而不是兩個:P – ErrorNotFoundException