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

梦想与她 提交于 2019-12-24 14:24:22

问题


Why does this work

SELECT DISTINCT FIRSTNAME, LASTNAME
FROM books, CUSTOMERS, orders, orderitems
WHERE STATE IN('FL ', 'GA')
GROUP BY orders.order#, firstname, lastname
HAVING SUM(retail*quantity) > 80  

but when firstname, lastname is removed from group by it doesn't?

ORA-00979: not a GROUP BY expression


回答1:


In that case where firstname, lastname are removed from the group by, you get that error because you're SELECTing a column(s) that aren't in the GROUP BY expression, or aren't part of an aggregation/function (i.e. MIN, MAX, AVG, and others).

You could also eliminate the DISTINCT as well.




回答2:


As I can guess

First of all GROUP BY operation is performed and then DISTINCT. In GROUP BY clause you must indicate all non-aggregates . For example you are not permitted to do the following:

SELECT FIRST_NAME, LAST_NAME
FROM EMPLOYEES
GROUP BY HIRE_DATE

You should do it by this way:

SELECT FIRST_NAME, LAST_NAME
FROM EMPLOYEES
GROUP BY HIRE_DATE,FIRST_NAME, LAST_NAME


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

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