我为Oracle做了这个任务。它要求我显示部门名称(部门是包含该表的manager_id,department_id和department_name的表),该部门的manager_id,经理的名称(在employees表中找到)以及该部门的平均工资(每个员工的工资也在员工中找到)。一旦我尝试检索经理名称(我想通过将他在部门中的ID与员工中的ID进行比较)就会弄乱我的平均值。没有它(如下),它可以正常工作
SELECT d.department_name, AVG(e.salary) as "Salaries"
FROM employees e join departments d on e.department_id=d.department_id
WHERE d.manager_id=e.employee_id
GROUP BY e.department_id, d.department_name,d.manager_id
ORDER BY AVG(e.salary)
有人可以帮我解决这个问题吗?或许可以解释为什么我搞砸了?
答案 0 :(得分:0)
在查询中选择管理员名称中引入相关子查询。
SELECT d.department_name,
AVG(e.salary) AS "Salaries",
(SELECT first_name
||' '
||last_name
FROM employees i
WHERE i.employee_id = d.manager_id) AS manager_name
FROM employees e
join departments d
ON e.department_id = d.department_id
GROUP BY e.department_id,
d.department_name,
d.manager_id
ORDER BY Avg(e.salary);
答案 1 :(得分:0)
WITH factoring clause选择平均工资;然后将它连接到其他表
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
快速检查2个部门:
SQL> with avgsal as
2 (select e.department_id, round(avg(e.salary)) avg_sal
3 from employees e
4 group by e.department_id
5 )
6 select d.department_id,
7 d.department_name,
8 m.last_name manager,
9 a.avg_sal
10 from departments d
11 join employees m on m.department_id = d.department_id
12 and m.employee_id = d.manager_id
13 join avgsal a on a.department_id = d.department_id
14 order by a.avg_sal;
DEPARTMENT_ID DEPARTMENT_NAME MANAGER AVG_SAL
------------- -------------------- ------------------------- ----------
50 Shipping Fripp 3476
30 Purchasing Raphaely 4150
10 Administration Whalen 4400
60 IT Hunold 5760
40 Human Resources Mavris 6500
100 Finance Greenberg 8601
80 Sales Russell 8956
20 Marketing Hartstein 9500
70 Public Relations Baer 10000
110 Accounting Higgins 10154
90 Executive King 19333
11 rows selected.
SQL>