我有两张桌子
EMPLOYEES(employee_id,first_name,last_name,salary,manager_id,department_id)
and
DEPARTMENTS(department_id,department_name,manager_id)
当我尝试创建一个新表时,#E; EMP_DEPT"其中包含
department_id ,department_name, dcount(count of employees in each department),
dtotal(total salary of employees in each department),
dmaxsal(maximum salary in a department), dminsal(minimum salary in a department)
它显示 ORA00979:不是GROUP BY表达式
我是在oracle中做到的
create table emp_dept as(select e.department_id,d.department_name,count(*),sum(salary),max(salary),min(salary)
from employees e,departments d where e.department_id= d.department_id
group by e.department_id);
答案 0 :(得分:0)
在编写查询时,您似乎有点困惑。
首先,您在表departments
的列中使用了别名,但您并未在表employees
的列中使用别名。
select e.department_id,d.department_name,count(*),sum(salary),max(salary),min(salary)
from employees e,departments d
其次,您使用的是where
条款,但不应该left join
更好的选择吗?
where e.department_id= d.department_id
第三,最重要的是,您在group by
中执行e.department_id
,而它应该是d.department_id
,因为表departments
之间存在父子关系employees
1}}和department_id
。我不知道你是如何设计你的表的,但从逻辑上讲,departments
应该是employees
表中的主键和group by e.department_id
表中的外键。这就是为什么, group by e.department_id
不正确。
因此,
group by d.department_id
应该是,
create table emp_dept as
(select d.department_id,d.department_name,count(e.*),
sum(e.salary),max(e.salary),min(e.salary)
from departments d
left join employees e
on e.department_id= d.department_id
group by d.department_id);
我认为您只需要按以下方式修改查询:
{{1}}
希望它能解决你的问题。