我希望能够按照员工最新项目的有效日期对数据进行分组,包括部门和他们工作的经理。这是一个数据样本。
PROJ_TBL
+-------------+----------+----------------+
| EMPLOYEE_ID | EFF_DATE | EMPL_PROJECT |
+-------------+----------+----------------+
| P1441 | 05/21/11 | IMC |
| P1441 | 09/12/12 | BEEB |
| P1441 | 09/23/12 | PRUD_FIN_SALES |
+-------------+----------+----------------+
EMPLOYEE_TBL
+-------------+--------------+---------+----------+
| EMPLOYEE_ID | PROJECT_MBR | DEPT_NM | EFF_DATE |
+-------------+--------------+---------+----------+
| P1441 | BEN DEENEY | ACCNT | 02/09/08 |
| P1566 | LAURA FIELDS | ACCNT | 05/03/10 |
| P2155 | PAUL DAVEY | ACCNT | 10/03/10 |
| P1441 | BEN DEENEY | SALES | 07/19/12 |
+-------------+--------------+---------+----------+
EMP_DPT_TBL
+-------------+---------------+---------+----------+
| EMPLOYEE_ID | MANAGER | DEPT_NM | EFF_DATE |
+-------------+---------------+---------+----------+
| P1441 | BOB PAISLEY | ACCNT | 02/09/08 |
| P1441 | LINDA HARDY | SALES | 07/19/12 |
+-------------+---------------+---------+----------+
我对分区的使用不是很熟悉。我希望能够将其用于合并信息,以获取EMPLOYEE_ID
P1441
的当前数据。所需的输出是:
+-------------+---------+--------------+----------------+
| EMPLOYEE_ID | DEPT_NM | MANAGER | PROJECT |
+-------------+---------+--------------+----------------+
| P1441 | SALES | LINDA HARDY | PRUD_FIN_SALES |
+-------------+---------+--------------+----------------+
我能够获得各个表的正确当前记录,但将结果组合起来生成我想要的内容是有问题的。以下是查询...
-- Current Project
SELECT EMPL_PROJECT,
EMPLOYEE_ID,
EFF_DT
FROM ( SELECT EMPL_PROJECT,
EMPLOYEE_ID,
EFF_DT,
RANK() OVER ( PARTITION BY EMPLOYEE_ID
ORDER BY EFF_DT DESC) AS rk1
FROM PROJ_TBL ) t
WHERE rk1 = 1
-- Current Department
SELECT DEPT_NM,
EMPLOYEE_ID,
EFF_DT
FROM ( SELECT DEPT_NM,
EMPLOYEE_ID,
EFF_DT,
RANK() OVER ( PARTITION BY EMPLOYEE_ID
ORDER BY EFF_DT DESC ) AS rk2
FROM EMPLOYEE_TBL ) t
WHERE rk2 = 1
-- Current Manager
SELECT MANAGER,
EMPLOYEE_ID,
EFF_DT
FROM ( SELECT MANAGER,
EMPLOYEE_ID,
EFF_DT,
RANK() OVER ( PARTITION BY EMPLOYEE_ID
ORDER BY EFF_DT DESC ) AS rk3
FROM EMP_DPT_TBL ) t
WHERE rk3 = 1
如何使用EMPLOYEE_ID
将这些组合成一个查询以生成报告?
答案 0 :(得分:0)
这是你要找的吗?
SELECT DISTINCT
EMPLOYEE_ID
,first_value(dept.dept_nm) over (partition by dept.employee_id order by dept.eff_date desc) as DEPT_NM
,first_value(dept.manager) over (partition by dept.employee_id order by dept.eff_date desc) as MANAGER
,first_value(proj.EMPL_PROJECT) over (partition by proj.employee_id order by proj.eff_date desc) as PROJECT
FROM
EMPLOYEE_TBL emp
INNER JOIN PROJ_TBL proj ON emp.EMPLOYEE_ID = proj.EMPLOYEE_ID
INNER JOIN EMP_DPT_TBL dept on emp.EMPLOYEE_ID = dept.EMPLOYEE_ID