我收到错误 ORA-00979: 不是 GROUP BY 表达式

i got error ORA-00979: not a GROUP BY expression

我在以下查询中遇到错误 ORA-00979

SELECT d.department_name "department name",
       e.job_id "job title",
       SUM(e.salary) "monthly cost", 
       GROUPING(d.department_id) "Department ID Used",
       GROUPING(e.job_id) "Job ID Used"
FROM   employees e
       JOIN departments d
       ON e.department_id=d.department_id
GROUP BY cube(d.department_name, e.job_id)
ORDER BY d.department_name, e.job_id

查询有问题吗?

您正在使用 GROUPING(d.department_id),但 department_id 不在 GROUP BY 子句中。

您可以将其添加到 GROUP BY 子句中:

SELECT d.department_name "department name",
       e.job_id "job title",
       SUM(e.salary) "monthly cost",
       GROUPING(d.department_id) "Department ID Used",
       GROUPING(e.job_id) "Job ID Used"
FROM   employees e
       JOIN departments d
       ON e.department_id=d.department_id
GROUP BY cube(d.department_name, d.department_id, e.job_id)
ORDER BY d.department_name, e.job_id

db<>fiddle here