尽管在 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;
甚至没有 where 作为第一个表的每条记录的全部总和(这里没有限制):
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 中做多个表副本的方法吗?