Recently I got involved in a question on Temporal Validity Periods together with Chris Saxon, one of the askTom-answer team.
The question was along the lines of: "What if I have a single start date but two possible end dates. One of the end dates is filled automatically by a background proces (could be a job) while the other one is to signal that the end date is set manually by the user. Could you use Temporal Validity to get the correct rows?"
My first reaction was: "Yes, you can have multiple validity periods on a single table... but wouldn't it be better to have a single end date and another column to signal if it was set by an automatic proces or by a manual process?". In the latter case you would only need a single validity period.
It got me thinking though... would it be possible to define a single validity period when three (one start and two end dates) are involved?
Let's start by creating a table and populate it with some sample data.
create table t
(id number primary key
,name varchar2(50) not null
,start_date date not null
,end_date date
,manual_end_date date
);
The table is fairly straight forward: a surrogate primary key, a name, a start date and two end dates: end_date and manual_end_date.
Next populate the table with some sample data. The name column is used to signal what the row represent, being a row that is current or not (based on sysdate).
insert into t (id, name, start_date) values (1, 'no end, current', sysdate - 10);
insert into t (id, name, start_date) values (2, 'no end, not current', sysdate + 10);
insert into t (id, name, start_date, end_date) values (3, 'current', sysdate - 10, sysdate + 10);
insert into t (id, name, start_date, end_date) values (4, 'not current, past', sysdate - 10, sysdate -9);
insert into t (id, name, start_date, end_date) values (5, 'not current, future', sysdate + 10, sysdate +20);
insert into t (id, name, start_date, manual_end_date) values (6, 'current', sysdate - 10, sysdate + 10);
insert into t (id, name, start_date, manual_end_date) values (7, 'not current, past', sysdate - 10, sysdate -9);
insert into t (id, name, start_date, manual_end_date) values (8, 'not current, future', sysdate + 10, sysdate +20);
commit;
select *
from t;
ID NAME START_DAT END_DATE MANUAL_EN ABSOLUTE_
---------- -------------------------------------------------- --------- --------- --------- ---------
1 current, no end 08-MAR-16
2 not current, no end 28-MAR-16
3 current 08-MAR-16 28-MAR-16 28-MAR-16
4 not current, past 08-MAR-16 09-MAR-16 09-MAR-16
5 not current, future 28-MAR-16 07-APR-16 07-APR-16
6 current 08-MAR-16 28-MAR-16 28-MAR-16
7 not current, past 08-MAR-16 09-MAR-16 09-MAR-16
8 not current, future 28-MAR-16 07-APR-16 07-APR-16
8 rows selected.
Now that the table is populated with the sample data, we can start the experiment. Can you have a temporal validity period based on an expression?
SQL> alter table t
2 add period for regular (start_date, coalesce (end_date, manual_end_date))
3 /
add period for regular (start_date, coalesce (end_date, manual_end_date))
*
ERROR at line 2:
ORA-02000: missing ) keyword
Uhm,... no.
What about moving the expression over to a virtual column and use the virtual column in the temporal validity period?
SQL> alter table t
2 add absolute_end_date generated always
3 as (coalesce (end_date, manual_end_date))
4 /
Table altered.
SQL> alter table t
2 add period for virt_col (start_date, absolute_end_date)
3 /
Table altered.
So far, so good. When playing around with temporal validity periods I ran into a bug by extracting the DDL using DBMS_METADATA, so let's see what Oracle makes of this combination of virtual column and the validity period.
SQL> set long 9000
SQL> select dbms_metadata.get_ddl ('TABLE', 'T')
2 from dual
3 /
DBMS_METADATA.GET_DDL('TABLE','T')
--------------------------------------------------------------------------------
CREATE TABLE "ALEX"."T"
( "ID" NUMBER,
"NAME" VARCHAR2(50) NOT NULL ENABLE,
"START_DATE" DATE NOT NULL ENABLE,
"END_DATE" DATE,
"MANUAL_END_DATE" DATE,
"ABSOLUTE_END_DATE" DATE GENERATED ALWAYS AS (COALESCE("END_DATE","MANUAL_END_D
ATE")) VIRTUAL ,
PERIOD FOR "VIRT_COL"("START_DATE","ABSOLUTE_END_DATE") ,
PRIMARY KEY ("ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"
As you can see from the output above, all seems to be just fine.
But now for the real test and query the table using the temporal validity.
SQL> select *
2 from t as of period for virt_col sysdate
3 /
ID NAME START_DAT END_DATE MANUAL_EN ABSOLUTE_
---------- -------------------------------------------------- --------- --------- --------- ---------
1 current, no end 08-MAR-16
3 current 08-MAR-16 28-MAR-16 28-MAR-16
6 current 08-MAR-16 28-MAR-16 28-MAR-16
That worked! Awesome!.. Virtual Columns with a Temporal validity period, wonderful.
I ran these test on my virtual box, using the pre-built developer appliance, so it might not be the latest patch release or anything.
SQL> select *
2 from v$version
3 /
BANNER CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production 0
PL/SQL Release 12.1.0.2.0 - Production 0
CORE 12.1.0.2.0 Production 0
TNS for Linux: Version 12.1.0.2.0 - Production 0
NLSRTL Version 12.1.0.2.0 - Production 0
Links