想象一下有锁门的房间。门口有读卡器。要打开门,您需要将卡放入读卡器。
我的 Oracle 数据库中有 2 个包含事件的表 - 条目(门是从房间外打开的)出口(门是从房间内打开的)
我想要的只是选择存在间隔,例如员工 1 在某天从 10:00 到 11:00 以及从 12:00 到 18:00 在房间里。
但是有3个问题
- 有时用户打开门但不离开房间。
- 有时一个用户打开门,另一位用户和他一起出去,而门打开一次。
- 数据大小(每张表约100k)
和问题:
- 选择存在间隔的最佳方法是什么
- 有没有办法创建快速可刷新的物化视图来解决这个问题?
这是样本
drop table entries;
drop table exits;
CREATE TABLE ENTRIES
(
"EVENTDATE" DATE NOT NULL,
"EVENTTIME" DATE NOT NULL,
"EMPLOYEEID" NUMBER NOT NULL
);
CREATE TABLE EXITS
(
"EVENTDATE" DATE NOT NULL,
"EVENTTIME" DATE NOT NULL,
"EMPLOYEEID" NUMBER NOT NULL
);
delete from ENTRIES;
delete from exits;
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 08:44:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:18:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:19:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:22:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:37:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:38:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:39:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:40:00','DD-MON-RR HH24:MI:SS'));
Insert into ENTRIES (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 13:22:00','DD-MON-RR HH24:MI:SS'));
Insert into EXITS (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:40:00','DD-MON-RR HH24:MI:SS'));
Insert into EXITS (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 12:36:00','DD-MON-RR HH24:MI:SS'));
Insert into EXITS (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 11:55:00','DD-MON-RR HH24:MI:SS'));
Insert into EXITS (EMPLOYEEID,EVENTDATE,EVENTTIME) values (8,to_date('01-AUG-13 00:00:00','DD-MON-RR HH24:MI:SS'),to_date('01-JAN-00 18:02:00','DD-MON-RR HH24:MI:SS'));
期望的结果是这样的:
"EMPLOYEID" "EVENTDATE" "ENTERTIME" "LEAVETIME"
8 01-AUG-13 00:00:00 01-JAN-00 08:44:00 01-JAN-00 11:55:00
8 01-AUG-13 00:00:00 01-JAN-00 12:18:00 01-JAN-00 12:36:00
8 01-AUG-13 00:00:00 01-JAN-00 12:37:00 01-JAN-00 12:40:00
8 01-AUG-13 00:00:00 01-JAN-00 13:22:00 01-JAN-00 18:02:00
更新
如果连续有 2 个输入,请先使用,然后忽略第二个。如果连续有 2 个出口,请使用第二个,首先忽略