ORA-00979: 不是 Oracle 中的 GROUP BY 表达式

ORA-00979: not a GROUP BY expression in Oracle

我无法在 Oracle 上执行此代码,错误显示:

"ORA-00979: not a GROUP BY expression"

但是,我能够 运行 它在 MySQL 上成功。
这是怎么发生的?

SELECT CONCAT(i.lname, i.fname) AS inst_name, 
           CONCAT(s.lname, s.fname) AS stu_name, 
           t.avg_grade AS stu_avg_grade
    FROM(
    SELECT instructor_id, student_id, AVG(grade) as avg_grade, RANK() OVER(PARTITION BY instructor_id ORDER BY grade DESC) AS rk
    FROM grade
    GROUP BY 1,2) t
    JOIN instructor i 
    ON t.instructor_id = i.instructor_id
    JOIN student s 
    ON s.student_id = t.student_id
    WHERE t.rk = 1
    ORDER BY 3 DESC

您不能在 Oracle 中使用像 GROUP BY 1,2 这样的序数。此外,RANK() 函数中的 ORDER BY grade 子句有问题。请记住,分析函数在 之后计算 GROUP BY 聚合,因此 grade 不再可用。这是一个应该可以正常工作的版本:

SELECT CONCAT(i.lname, i.fname) AS inst_name,
       CONCAT(s.lname, s.fname) AS stu_name,
       t.avg_grade AS stu_avg_grade
FROM
(
    SELECT instructor_id, student_id, AVG(grade) AS avg_grade,
           RANK() OVER (PARTITION BY instructor_id ORDER BY AVG(grade) DESC) AS rk
    FROM grade
    GROUP BY instructor_id, student_id
 ) t
INNER JOIN instructor i 
    ON t.instructor_id = i.instructor_id
INNER JOIN student s 
    ON s.student_id = t.student_id
WHERE t.rk = 1
ORDER BY t.avg_grade DESC;