2017-06-02 100 views
0

我希望能夠按照員工最新項目的生效日期對數據進行分組,包括他們工作的部門和經理。這是一個數據樣本。Oracle SQL分區和排名

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 | 
+-------------+---------------+---------+----------+ 

我不是很福美來r使用分區。我希望能夠使用它來組合信息以獲得當前數據EMPLOYEE_IDP1441。所需的輸出是:

+-------------+---------+--------------+----------------+ 
| 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

完全無關的問題,但你爲什麼,在'employee_tbl',同時存儲員工的ID和他或她的名字?更好的(規範化)設計是將僱員ID作爲主鍵的不同表格(所以不重複!)以及每個ID和僱員的姓名。這實際上被稱爲「employee_tbl」。那麼你作爲第二張表顯示的是員工分配到不同部門的歷史表格;這個歷史表應該只有員工ID,而不是名稱 - 對部門來說也是一樣的(只包括部門ID並且有單獨的DEPARTMENTS_TBL)。 – mathguy

回答

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 
+0

我的不好,它似乎在工作,讓我再做幾次檢查 – BreenDeen