ORA-00979: not a GROUP BY expression [duplicate]

浪尽此生 提交于 2019-12-20 04:44:05

问题


I am trying show all the different companies for which students work. However only companies where more than four students are employed should be displayed.

This is what I have so far:

SELECT EMPLOYER, COUNT (STUDENT_ID)
FROM STUDENT
GROUP BY STUDENT_ID
HAVING COUNT (STUDENT_ID) >4;

I keep getting this message:

ERROR at line 1: 
ORA-00979: not a GROUP BY expression

I don't get it. I also tried this earlier:

SELECT STUDENT.EMPLOYER, COUNT (STUDENT.STUDENT_ID)
FROM STUDENT
GROUP BY STUDENT.STUDENT_ID
HAVING COUNT (STUDENT.STUDENT_ID) >4;

but nothing seems to work. Any help is appreciated. I am on SQL*Plus if that helps.


回答1:


Try:

SELECT EMPLOYER, COUNT (STUDENT_ID)
FROM STUDENT
GROUP BY EMPLOYER
HAVING COUNT (STUDENT_ID) >4;

- this will return a list of all employers with more than 4 students.

When grouping or including aggregated fields, your select statement should only include fields that are either aggregated or included in the group by clause - in your existing select, you are including EMPLOYER in your select clause, but not grouping by it or aggregating it.



来源:https://stackoverflow.com/questions/15865146/ora-00979-not-a-group-by-expression

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!