2012-05-25 47 views
1

請看下面的圖片。它基本上是我數據庫的外觀。如何獲取不同生效日期的員工詳細信息?

BI_EMPLOYEE表總是有今天的記錄。雖然它可能在某個以前的日期有效。

例如如圖所示的員工Anna從3月31日加入,直到今天沒有對該員工進行更新。

因此,BI_EMPLOYEE有生效日期= 3月31日2012年

但是,後來可能會有變動對將來的日期的員工現在是知道的。

例如安娜在5月10日從亞洲轉移到我們,等等。所以,從10月開始,她的地區將成爲我們。

因此,我需要一份報告,其中列出了所有員工在每個生效日期的狀態。基本上,所有emplyees所有活動和更新日期。

我們還每天運行一次調度,它會運行所有更新和活動,即從更新和活動表向BI_EMPLOYEE TABLE執行chnages。

因此,截至5月10日,地塊將從亞洲轉向我們。

我也提出了預期結果的圖像。

我們正在使用Oracle數據庫。

請幫助。

enter image description here enter image description here

enter image description hereenter image description here

enter image description here

我將基本上需要創建一個視圖,這將得到期望的輸出然後可以被查詢任何時間以任何報告。

EDIT 1:

以下是在DDL的和DML的

 -------------------------------------------------------- 
    -- DDL for Table BI_REGION 
    -------------------------------------------------------- 
    CREATE TABLE "BI_REGION" 
     ( "REGION_ID" NUMBER(*,0) NOT NULL ENABLE, 
     "REGION_NAME" VARCHAR2(4000) NOT NULL ENABLE 
     ) ; 
    -------------------------------------------------------- 
    -- DATA FOR TABLE BI_REGION 
    -------------------------------------------------------- 
    Insert into BI_REGION (REGION_ID,REGION_NAME) values (1,'Asia'); 
    Insert into BI_REGION (REGION_ID,REGION_NAME) values (2,'US'); 
    Insert into BI_REGION (REGION_ID,REGION_NAME) values (3,'UK'); 
    Insert into BI_REGION (REGION_ID,REGION_NAME) values (4,'Germany'); 
    Insert into BI_REGION (REGION_ID,REGION_NAME) values (5,'EUROPE'); 
    Insert into BI_REGION (REGION_ID,REGION_NAME) values (6,'AUSTRALIA'); 

    ------------------------------------------------------- 
    -- DDL for Table BI_COUNTRY 
    -------------------------------------------------------- 
     CREATE TABLE "BI_COUNTRY" 
     ( "COUNTRY_ID" NUMBER(*,0) NOT NULL ENABLE, 
     "COUNTRY_NAME" VARCHAR2(4000) NOT NULL ENABLE 
     ) ; 

    -- DATA FOR TABLE BI_COUNTRY 
    -- FILTER = none used 
    --------------------------------------------------- 
    REM INSERTING into BI_COUNTRY 
    Insert into BI_COUNTRY (COUNTRY_ID,COUNTRY_NAME) values (1,'India'); 
    Insert into BI_COUNTRY (COUNTRY_ID,COUNTRY_NAME) values (2,'USA'); 
    Insert into BI_COUNTRY (COUNTRY_ID,COUNTRY_NAME) values (3,'England'); 
    Insert into BI_COUNTRY (COUNTRY_ID,COUNTRY_NAME) values (4,'Germany'); 
    Insert into BI_COUNTRY (COUNTRY_ID,COUNTRY_NAME) values (5,'New Zealnd'); 

    -------------------------------------------------------- 
    -- DDL for Table BI_DIVISION 
    -------------------------------------------------------- 
     CREATE TABLE "BI_DIVISION" 
     ( "DIVISION_ID" NUMBER(*,0) NOT NULL ENABLE, 
     "DIVISION_NAME" VARCHAR2(4000) 
     ) ; 

    --------------------------------------------------- 
    -- DATA FOR TABLE BI_DIVISION 
    -- FILTER = none used 
    --------------------------------------------------- 

    Insert into BI_DIVISION (DIVISION_ID,DIVISION_NAME) values (1,'D1'); 
    Insert into BI_DIVISION (DIVISION_ID,DIVISION_NAME) values (2,'D2'); 


    -------------------------------------------------------- 
    -- DDL for Table BI_PRODUCT 
    -------------------------------------------------------- 
     CREATE TABLE "BI_PRODUCT" 
     ( "PRODUCT_NAME" VARCHAR2(4000), 
     "PRODUCT_ID" NUMBER(*,0) NOT NULL ENABLE 
     ) ; 

    --------------------------------------------------- 
    -- DATA FOR TABLE BI_PRODUCT 
    -- FILTER = none used 
    --------------------------------------------------- 
    REM INSERTING into BI_PRODUCT 
    Insert into BI_PRODUCT (PRODUCT_NAME,PRODUCT_ID) values ('P1','1'); 
    Insert into BI_PRODUCT (PRODUCT_NAME,PRODUCT_ID) values ('P2','2'); 


    -------------------------------------------------------- 
    -- DDL for Table BI_EMPLOYEE_ACTIVITY 
    -------------------------------------------------------- 
     CREATE TABLE "BI_EMPLOYEE_ACTIVITY" 
     ( "ACTIVITY_ID" NUMBER(*,0) NOT NULL ENABLE, 
     "STATUS" NUMBER(*,0), 
     "STATUS_2" NUMBER(*,0),  
     "STATUS_3" VARCHAR2(4000), 
     "CONFIDENTIAL" VARCHAR2(1 CHAR) DEFAULT 'N', 
     "EFFECTIVE_DATE" DATE, 
     "PARENT_ACTIVITY_ID" NUMBER(*,0), 
     "EXECUTED" VARCHAR2(1 CHAR) DEFAULT 'N', 
     "EMPLOYEE_ID" NUMBER 
     ) ; 



    --------------------------------------------------- 
    -- DATA FOR TABLE BI_EMPLOYEE_ACTIVITY 
    -- FILTER = none used 
    --------------------------------------------------- 

    REM INSERTING into BI_EMPLOYEE_ACTIVITY 
    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (1,1,'Leaver','Intra Region','','No','10-May-2012',0); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (2,1,'Joiner','Intra Region','','No','10-May-2012',1); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (3,1,'Leaver','Intra Region','','No','10-June-2012',0); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (4,1, 'Joiner','Intra Region','','No','10-June-2012',3); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (5,1,'Leaver','Intra Region','','No','10-July-2012',0); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (6,1, 'Joiner','Intra Region','','No','10-July-2012',5); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (7,1,'Leaver','Intra Business','','No','10-Aug-2012',0); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (8,1, 'Joiner','Intra Business','','No','10-Aug-2012',7); 

    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (9,1,'Leaver','Intra Business','','No','10-Sep-2012',0); 


    Insert into BI_EMPLOYEE_ACTIVITY (ACTIVITY_ID,EMPLOYEE_ID, STATUS,STATUS_2,STATUS_3,CONFIDENTIAL,EFFECTIVE_DATE,PARENT_ACTIVITY_ID,) 
    values (10,1, 'Joiner','Intra Business','','No','10-Sep-2012',9); 




    -------------------------------------------------------- 
    -- DDL for Table BI_EMPLOYEE 
    -------------------------------------------------------- 
     CREATE TABLE "BI_EMPLOYEE" 
     ( "EMP_ID" NUMBER(*,0) NOT NULL ENABLE, 
     "FIRSTNAME" VARCHAR2(4000), 
     "LASTNAME" VARCHAR2(4000), 
     "DIVISION_ID" NUMBER(*,0), 
     "PRODUCT_ID" NUMBER(*,0), 
     "REGION_ID" NUMBER(*,0), 
     "COUNTRY_ID" NUMBER(*,0), 
     "CITY" VARCHAR2(4000) , 
     "EFFECTIVE_DATE" DATE 
     ) ; 

    --------------------------------------------------- 
    -- DATA FOR TABLE BI_EMPLOYEE 
    -- FILTER = none used 
    --------------------------------------------------- 
    Insert into BI_EMPLOYEE (EMP_ID,FIRSTNAME,LASTNAME,DIVISION_ID,PRODUCT_ID,REGION_ID,COUNTRY_ID,EFFECTIVE_DATE,CITY) values (1,'Ana','Johnston',1,1,1,1,'31-March-2012','Mumbai'); 


    -------------------------------------------------------- 
    -- DDL for Table BI_EMPLOYEE_UPDATE 
    -------------------------------------------------------- 
     CREATE TABLE "BI_EMPLOYEE_UPDATE" 
     ( "EMPLOYEE_UPDATE_ID" NUMBER(*,0) NOT NULL ENABLE, 
     "EMPLOYEE_ID" NUMBER(*,0), 
     "COLUMN_NAME" VARCHAR2(4000), 
     "OLD_VALUE" VARCHAR2(4000), 
     "NEW_VALUE" VARCHAR2(4000), 
     "EFFECTIVE_DATE" DATE, 
     "EXECUTED" VARCHAR2(1 CHAR) DEFAULT 'N', 
     "ACTIVITY_ID" NUMBER 
     ) ; 


    --------------------------------------------------- 
    -- DATA FOR TABLE BI_EMPLOYEE_UPDATE 
    -- FILTER = none used 
    --------------------------------------------------- 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'REGION_ID','1','2','10-May-2012', 'N', 1); 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'COUNTRY_ID','1','2','10-May-2012', 'N', 1); 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'CITY','Mumbai','New York','10-May-2012', 'N', 1); 

    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'REGION_ID','2','3','10-June-2012', 'N', 3); 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'COUNTRY_ID','2','3','10-June-2012', 'N', 3); 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'CITY','New York','London','10-June-2012', 'N', 3); 

    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'REGION_ID','3','4','10-July-2012', 'N', 5); 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'COUNTRY_ID','3','4','10-July-2012', 'N', 5); 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'CITY','London','frankfurt','10-July-2012', 'N', 5); 

    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'DIVISION_ID','1','2','10-Aug-2012', 'N', 7); 
    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'Product_ID','1','2','10-Aug-2012', 'N', 7); 

    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'PRODUCT_ID','2','3','10-Sep-2012', 'N', 9); 


    Insert into BI_EMPLOYEE_UPDATE (EMPLOYEE_ID,COLUMN_NAME,OLD_VALUE,NEW_VALUE,EFFECTIVE_DATE,EXECUTED,ACTIVITY_ID) values (1,'LASTNAME','Johnston','thomas','10-Nov-2012', 'N', 0); 

+0

爲什麼關閉?我一直在嘗試這個很長一段時間沒有運氣:( – ashishjmeshram

+0

不贊同近距離投票 - 我已經遇到過這樣的需求一次或兩次自己。 –

+0

@Jeffrey肯普。嗨。你能解決它嗎?任何對此有何建議? – ashishjmeshram

回答

1

首先,較好的數據結構是具有在數據的有效日期和結束日期。通過這種方式,您可以查看在任何給定日期哪個記錄處於活動狀態。

幸運的是,你可以得到的結束日期在Oracle中以下查詢:

select eu.*, 
     (lead(effective_date, 1, '9999-01-01') over (partition by employee_id, column_name 
             order by effective_date)) - 1) as end_date 
from bi_employee_update 

這個查詢着眼於接下來的生效日期,使用「鉛」分析功能。然後它減去一天得到當前記錄的結束日期,當前記錄的默認值爲01Jan9999。

使用此查詢,您可以通過使用得到有關任何日期的emmployee信息的條款之間:

select eu.* 
from (select eu.*, 
      (lead(effective_date, 1, '9999-01-01') over (partition by employee_id, column_name 
             order by effective_date)) - 1) as end_date 
     from bi_employee_update 
    ) eu 
where <date> between effective_date and end_date 

你能不能從這裏走查詢?

+0

感謝您的回答。對不起,但我不確定我是否完全理解。 :( – ashishjmeshram

+0

我改寫了一些東西......有沒有幫助? –

+0

對不起,沒有運氣:(還有,我不想通過任何日期查詢,只需獲取所有員工在不同日期的所有數據。 – ashishjmeshram