尽管在PostgreSQL中工作,为什么不选择在Oracle中工作(第三个总和应该是相同的:从员工中选择总和(薪水))?:
select e1.employee_id, e1.department_id, e1.manager_id, e1.salary,
sum(e2.salary), sum(e3.salary)
from employees e1, employees e2, employees e3
where e1.department_id=e2.department_id
group by e1.employee_id, e1.department_id, e1.manager_id, e1.salary
order by 1,2;
它适用于PostgreSQL,但不适用于此。在Oracle中,只有一个副本e2没有任何约束(当e1是原始表并按主键分组时):
select e1.employee_id, e1.department_id, e1.manager_id, e1.salary,
sum(e2.salary)
from employees e1, employees e2
where e1.department_id=e2.department_id
group by e1.employee_id, e1.department_id, e1.manager_id, e1.salary
order by 1,2;
甚至没有作为第一张表的每条记录的总和(这里没有约束):
select e1.employee_id, e1.department_id, e1.manager_id, e1.salary,
sum(e2.salary)
from employees e1, employees e2
group by e1.employee_id, e1.department_id, e1.manager_id, e1.salary
order by 1,2;
当然,我知道其他选择,例如:
select employee_id, department_id, manager_id, salary,
sum(salary) over (partition by department_id) as suma,
sum(salary) over ()
from employees order by 2,3;
或使用子选择:
select employee_id, department_id, manager_id, salary,
(select sum(e2.salary) from employees e2
where e2.department_id=e1.department_id) as suma1,
(select sum(e2.salary) from employees e2) as suma2
from employees e1 order by 1,2;
您是否知道在Oracle中执行多个表副本的方法?
答案 0 :(得分:1)
在您的第一个查询中,您创建了一个完整的笛卡尔积,因为您没有设置任何约束来匹配表