Advertisement
CSenshi

DB ESM - HW2

Jul 22nd, 2020
1,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 0.96 KB | None | 0 0
  1. SELECT
  2.     cc.country_id AS COUNTRY_CODE,
  3.     Nvl(emp.job_id, 'N\A') AS JOB,
  4.     To_char(Avg(emp.salary), 'fm$999,999,990.00') AS AVG_SALARY,
  5.     (SELECT Max(salary) FROM hr.employees in_emp
  6.         JOIN hr.departments in_dep ON in_dep.department_id = in_emp.department_id
  7.         JOIN hr.locations in_loc ON in_loc.location_id = in_dep.location_id
  8.         WHERE in_loc.country_id = cc.country_id)    AS MAX_SALARY,
  9.     Count(CASE WHEN tr.employee_id = emp.employee_id THEN 1 ELSE NULL END)AS JOB_CHANGES
  10. FROM hr.employees emp
  11.     INNER JOIN
  12.         hr.departments dp ON dp.department_id = emp.department_id
  13.     INNER JOIN
  14.         hr.locations loc ON loc.location_id = dp.location_id
  15.     RIGHT JOIN
  16.         hr.countries cc ON cc.country_id = loc.country_id
  17.     LEFT JOIN
  18.     (SELECT DISTINCT jh.employee_id FROM hr.job_history jh) tr ON tr.employee_id = emp.employee_id
  19. GROUP  BY cc.country_id, emp.job_id
  20. ORDER  BY (CASE WHEN cc.country_id = 'US' THEN 0 ELSE 1 END),
  21.           (CASE WHEN emp.job_id IS NULL THEN 1 ELSE 0 END);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement