2017-10-05 20 views
1

我正在處理此任務,需要爲每個學生計算CGPA,以及Grades * credits /總學分公式。非法組功能SQL

select 
    round(sum((grade.grade*course.credits)/count(course.courseno)),1), 
    student.studentNo 
from 
    course, grade, student, offering 
where 
    student.studentno = grade.studentno 
    and course.courseno = offering.courseno 
    and offering.offerno = grade.offerno 
group by 
    student.studentNo; 

我有點卡住無法繼續,因爲我不知道這是什麼問題。

編輯:所以我做了一些更改爲你們指出,但即時通訊仍處於困境

select grade.studentno, 
(select sum (t) 
from(select(grade*credits)/count(offering.courseno) 
    from student s2, grade g2, offering o2, course c2 
    where s2.studentno=g2.studentno 
    and g2.offerno=o2.offerno 
    and o2.courseno=c2.courseno 
    group by g2.studentno 
    ) t 
) 
from student, grade, offering, course 
where student.studentno=grade.studentno 
and grade.offerno=offering.offerno 
and offering.courseno=course.courseno 
group by grade.studentno; 

錯誤

編號1054:在「字段列表」

未知列offering.courseno

它無法識別'count'子查詢中的聚合

+0

您不能只使用'AVG(grade.grade * course.credits)'? –

+0

[踢壞的習慣:使用舊式JOIN](http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins。 aspx) - 在ANSI - ** 92 ** SQL標準(** 25年**之前!)中,舊式*逗號分隔的表*樣式列表被替換爲* proper * ANSI'JOIN'語法並且不鼓勵使用 –

回答

0

不允許在同一個SELECT中嵌套聚合函數,如SUM(X/COUNT(Y))。當需要嵌套時,可以使用子查詢來執行一個查詢,然後使用外部查詢來執行另一個查詢;無論如何這通常是必需的,因爲這樣的場景通常需要不同的分組標準。

+0

好了,所以我試圖做出一些改變,但它仍然沒有工作: 選擇grade.studentno, \t(選擇總和(T) \t從(選擇(等級*學分)/數(產品。 courseno) \t \t from student s2,grade g2,offer o2,course c2 \t \t where s2.studentno = g2.studentno \t \t和g2.offerno = o2.offerno \t \t和o2.courseno = c2.courseno \t \t組由g2.studentno \t \t)噸 ) 從學生,等級,提供,當然 其中學生。 studentno = grade.studentno and grade.offerno = offering.offerno and offering.courseno = course.courseno group by grade.studentno; –

0

除了ONLY_FULL_GROUP_BY模式外,不允許在同一個SELECT中嵌套聚合函數。您可以使用子查詢來選擇與此類似的兩列

select round(sum((grade.grade*course.credits)/count(course.courseno)),1) from (select student.studentNo from course, grade, student, offering where student.studentno=grade.studentno and course.courseno=offering.courseno and offering.offerno=grade.offerno) group by student.studentNo;