如何显示HR所有下属的名字,姓氏和工资。神谕

时间:2015-12-15 21:46:17

标签: sql oracle oracle11g

如何在Oracle上显示HR的所有下属的名字,姓氏和工资?

我有:

SELECT J.JOB_TITLE, E.FIRST_NAME, E.LAST_NAME, E.SALARY, (SELECT SUM(EMPLOYEES.SALARY) FROM EMPLOYEES)
FROM JOBS J LEFT JOIN EMPLOYEES E ON J.JOB_ID = E.JOB_ID 
GROUP BY J.JOB_TITLE, E.FIRST_NAME, E.LAST_NAME, E.SALARY
ORDER BY E.SALARY DESC;

但它显示了总薪水:

SELECT SUM(EMPLOYEES.SALARY) FROM EMPLOYEES

架构: enter image description here

THX。

3 个答案:

答案 0 :(得分:0)

我认为你正在寻找这样的东西:

SELECT 
  J.JOB_TITLE, 
  E.FIRST_NAME, 
  E.LAST_NAME, 
  E.SALARY, 
  (SELECT SUM(EMPLOYEES.SALARY) FROM EMPLOYEES where manager_id = e.employee_id) 
FROM 
  JOBS J LEFT JOIN EMPLOYEES E ON J.JOB_ID = E.JOB_ID 
GROUP BY 
  J.JOB_TITLE, E.FIRST_NAME, E.LAST_NAME, E.SALARY
ORDER BY 
  E.SALARY DESC;

如果您正在寻找,请告诉我。

答案 1 :(得分:0)

如果您希望每个员工的薪水,为什么要使用SUM功能?您是否可以对您实际尝试获取的内容以及从您提供的查询中未获得的内容进行更具描述性的描述?

我猜你只想要HR员工的工资,所以你需要添加一个where子句。您需要检查数据并查看实际的部门名称。是“人力资源”“人力资源”等 即

WHERE E.DEPARTMENT_ID = 'HR' 

所以试试这个:

SELECT J.JOB_TITLE, E.FIRST_NAME, E.LAST_NAME, E.SALARY, (SELECT SUM(EMPLOYEES.SALARY) FROM EMPLOYEES)
FROM JOBS J LEFT JOIN EMPLOYEES E ON J.JOB_ID = E.JOB_ID 
WHERE E.DEPARTMENT_ID = 'HR'  
GROUP BY J.JOB_TITLE, E.FIRST_NAME, E.LAST_NAME, E.SALARY
ORDER BY E.SALARY DESC;

答案 2 :(得分:0)

所以,如果我做对了,你希望你的最后一列是所有下属员工的总薪水,无论是直接还是间接。如果是这样,您可以在子查询中使用connect by子句来获取该信息。

尝试这样的事情:

SELECT j.job_title,
       e.first_name.
       e.last_name,
       e.salary,
       (
          SELECT SUM(s.salary)
            FROM employees s
           START WITH s.manager_id = e.employee_id,
         CONNECT BY PRIOR s.employee_id = s.manager_id
       ) AS total_subordinate_salary
  FROM jobs j 
  LEFT JOIN employees e
    ON j.job_id = e.job_id
 GROUP BY j.job_title, e.employee_id, e.first_name, e.last_name, e.salary
 ORDER BY e.salary DESC NULLS LAST;