GROUP BY
clause, but one or more non-aggregated columns in the SELECT
clause are not included in the GROUP BY
clause.Here's an example that demonstrates the issue:
SELECT dept_name, employee_name, AVG(salary)
FROM employees
GROUP BY dept_name;
This query calculates the average salary for each department in the employees
table, but it includes the non-aggregated employee_name
column in the SELECT
clause without including it in the GROUP BY
clause. This will result in the ORA-00979 error, because employee_name
is not included in the GROUP BY
clause and it's not part of an aggregate function.
To fix this error, you need to include all non-aggregated columns in the SELECT
clause in the GROUP BY
clause as well. Here's the corrected query:
SELECT dept_name, employee_name, AVG(salary)
FROM employees
GROUP BY dept_name, employee_name;
In this query, we include the employee_name
column in the GROUP BY
clause along with the dept_name
column. This ensures that all non-aggregated columns in the SELECT
clause are included in the GROUP BY
clause, and the AVG
function can be calculated for each combination of dept_name
and employee_name
.
No comments:
Post a Comment