/**************************************************************************************/
/* File:        demo30                                                                */
/* Project:     ATSQL2 : Bitemporal Negation                                          */
/* Author:      Andreas Steiner                                                       */
/* Date:        19-06-1995                                                            */
/* Results:                                                                           */
/**************************************************************************************/

SET CALENDRIC SYSTEM decimal;

create table employee(eno integer, name char(20)) as valid and transaction;
create table salary(eno integer, amount integer) as valid and transaction;

set clock to timestamp '1';
nonsequenced valid period '1-4'
insert into employee values(9999, 'Maya');

set clock to timestamp '2';
nonsequenced valid period '2-3'
insert into salary values(9999, 4000);

set clock to timestamp '3';
nonsequenced valid 
delete from salary;

set clock to timestamp '4';
nonsequenced valid 
delete from employee;


valid and transaction select * from employee;

valid and transaction select * from salary;

/* Which employee did not have a salary, when was this and when
   was it stored ? */
valid and transaction
select * 
from   employee e 
where not exists(select * 
                 from   salary s 
                 where  s.eno=e.eno);

/*
                                  TRANSACTION                                        VALID             eno                name
 -------------------------------------------- -------------------------------------------- --------------- -------------------
                                      [1 - 2)                                      [1 - 4)            9999                Maya
                                      [3 - 4)                                      [1 - 4)            9999                Maya
                                      [2 - 3)                                      [1 - 2)            9999                Maya
                                      [2 - 3)                                      [3 - 4)            9999                Maya
*/


/* In what states of the database did which employee not have a 
   salary for more than 2 years and when was this ? */

valid and transaction
select *
from   (valid and transaction
          (select * 
           from   employee e 
           where not exists(select * 
                            from   salary s 
                            where  s.eno=e.eno)) (PERIOD)) a1
where  interval(valid(a1)) > interval '2' year;

/*
                                  TRANSACTION                                        VALID             eno                name
 -------------------------------------------- -------------------------------------------- --------------- -------------------
                                      [1 - 2)                                      [1 - 4)            9999                Maya
                                      [3 - 4)                                      [1 - 4)            9999                Maya
*/


/* During which transaction times did we store more than 2 years 
   that an employee did not have a salary ? */

valid and transaction
select *
from   (transaction and valid 
           (select * 
            from   employee e 
            where not exists(select * 
                             from   salary s 
                             where  s.eno=e.eno)) (PERIOD)) a1
where interval(transaction(a1)) > interval '2' year;

/*
                                  TRANSACTION                                        VALID             eno                name
 -------------------------------------------- -------------------------------------------- --------------- -------------------
                                      [1 - 4)                                      [1 - 2)            9999                Maya
                                      [1 - 4)                                      [3 - 4)            9999                Maya
*/


drop table employee;
drop table salary;


create table employee(eno integer, name char(20)) as valid and transaction;
create table salary(eno integer, amount integer) as valid and transaction;

set clock to timestamp '6';
nonsequenced valid period '1-6'
insert into employee values(9999, 'Maya');

set clock to timestamp '9';
nonsequenced valid period '4-7'
insert into salary values(9999, 4000);

set clock to timestamp '11';
nonsequenced valid 
delete from employee;

set clock to timestamp '13';
nonsequenced valid 
delete from salary;


valid and transaction select * from employee;

valid and transaction select * from salary;

/* Which employee did not have a salary, when was this and when
   was it stored ? */
valid and transaction
select * 
from   employee e 
where not exists(select * 
                 from   salary s 
                 where  s.eno=e.eno);

/*
                                  TRANSACTION                                        VALID             eno                name
 -------------------------------------------- -------------------------------------------- --------------- -------------------
                                      [6 - 9)                                      [1 - 6)            9999                Maya
                                     [9 - 11)                                      [1 - 4)            9999                Maya
*/

drop table employee;
drop table salary;
