请参阅以下图片。它本质上是我的数据库的样子。
BI_EMPLOYEE表始终具有当前记录。虽然从之前的某个日期开始可能有效。
e.g。图片中显示的员工Anna从3月31日开始加入,直到今天,该员工没有更新。
因此,BI_EMPLOYEE的生效日期为2012年3月31日。
但是,稍后可能会在未来的日期向员工发起变动,现在就知道了。
e.g。 5月10日,安娜正从亚洲移民到我们,依此类推。所以,从10岁开始,她的地区将成为我们。
因此,我需要一份报告,该报告将向我提供所有员工的列表,其中包含每个生效日期的状态。基本上,所有人都在各个日期进行所有活动和更新。
我们还每天运行一个调度程序来运行所有更新和活动,即从更新和活动表中为BI_EMPLOYEE TABLE创建chnages。
因此,截至5月10日,计划将把区域从亚洲改为我们。
我还拍摄了一张预期结果的图片。
我们正在使用Oracle数据库。
请帮忙。
我基本上需要创建一个视图,该视图将提供所需的输出,然后可以随时查询任何报告。
编辑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 :(得分: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。
使用此查询,您可以使用between子句在任何日期获取有关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
你能从这里接受查询吗?