SELECT s.dept AS 部门, m.male_sal AS 男薪水, f.female_sal AS 女薪水, t.total_sal AS 合计薪水 FROM sett.t_staff_info s LEFT JOIN ( SELECT dept, SUM(sal) AS male_sal FROM sett.t_staff_info WHERE sex = '0' GROUP BY dept ) m ON s.dept =m.dept LEFT JOIN ( SELECT dept, SUM(sal) AS female_sal FROM sett.t_staff_info WHERE sex = '1' GROUP BY dept ) f ON s.dept = f.dept LEFT JOIN ( SELECT dept, SUM(sal) AS total_sal FROM sett.t_staff_info GROUP BY dept ) t ON s.dept = t.dept GROUP BY s.dept; 报错:ORA-00979:不是 GROUP BY表达式
时间: 2024-04-10 17:28:24 浏览: 141
根据你提供的SQL语句和报错信息,问题出在GROUP BY子句上。要解决这个问题,将所有的非聚合列都添加到GROUP BY子句中。修改后的SQL语句如下:
```sql
SELECT s.dept AS 部门,
m.male_sal AS 男薪水,
f.female_sal AS 女薪水,
t.total_sal AS 合计薪水
FROM sett.t_staff_info s
LEFT JOIN (
SELECT dept, SUM(sal) AS male_sal
FROM sett.t_staff_info
WHERE sex = '0'
GROUP BY dept
) m ON s.dept = m.dept
LEFT JOIN (
SELECT dept, SUM(sal) AS female_sal
FROM sett.t_staff_info
WHERE sex = '1'
GROUP BY dept
) f ON s.dept = f.dept
LEFT JOIN (
SELECT dept, SUM(sal) AS total_sal
FROM sett.t_staff_info
GROUP BY dept
) t ON s.dept = t.dept
GROUP BY s.dept, m.male_sal, f.female_sal, t.total_sal;
```
这样,所有的非聚合列都被添加到了GROUP BY子句中,应该能够解决ORA-00979错误。请尝试使用修改后的SQL语句。
阅读全文