/**************************************************************************************/
/* File:        demo19                                                                */
/* Project:     ATSQL2                                                                */
/* Author:      Nicola Piccioni, Andreas Steiner                                      */
/* Date:        30-09-1996                                                            */
/* Results:                                                                           */
/**************************************************************************************/

SET CALENDRIC SYSTEM fiscal;

create table p(i integer);
create table q(i integer);

select *
from p
where not exists(select *
                 from q
                 where p.i=q.i or p.i=q.i);





CREATE TABLE upward(AcctNo INTEGER,
                    AccHol VARCHAR(10),
                    Balance INTEGER,
                    AnIntRate INTEGER) AS VALID;


CREATE TABLE insett(sett VARCHAR(10)) AS VALID;


CREATE TABLE inmesi(mese VARCHAR(10)) AS VALID;




NONSEQUENCED VALID PERIOD '1993/08-1993/09' 
INSERT INTO upward VALUES (1001,'J.Smith',4000,4.00);

NONSEQUENCED VALID PERIOD '1993/04-1993/07' 
INSERT INTO upward VALUES (1001,'J.Smith',3500,4.00);

NONSEQUENCED VALID PERIOD '1993/04-1993/09'
INSERT INTO upward VALUES (1500,'A.Brady',1500,3.00);

NONSEQUENCED VALID PERIOD '1993/04-1993/09'
INSERT INTO upward VALUES (2034,'T.Ford',500,2.50);


NONSEQUENCED VALID PERIOD '1993/01/04-1993/01/11'
 INSERT INTO insett VALUES ('prima93');
NONSEQUENCED VALID PERIOD '1993/01/11-1993/01/18'
 INSERT INTO insett VALUES ('seconda93');
NONSEQUENCED VALID PERIOD '1993/01/18-1993/01/25'
 INSERT INTO insett VALUES ('terza93');
NONSEQUENCED VALID PERIOD '1993/01/25-1993/02/01'
 INSERT INTO insett VALUES ('quarta93');
NONSEQUENCED VALID PERIOD '1993/02/01-1993/02/08'
 INSERT INTO insett VALUES ('quinta93');
NONSEQUENCED VALID PERIOD '1993/02/08-1993/02/15'
 INSERT INTO insett VALUES ('sesta93');
NONSEQUENCED VALID PERIOD '1993/02/15-1993/02/22'
 INSERT INTO insett VALUES ('settima93');
NONSEQUENCED VALID PERIOD '1993/02/22-1993/03/01'
 INSERT INTO insett VALUES ('ottava93');
NONSEQUENCED VALID PERIOD '1993/03/01-1993/03/08'
 INSERT INTO insett VALUES ('nona93');
NONSEQUENCED VALID PERIOD '1993/03/08-1993/03/15'
 INSERT INTO insett VALUES ('decima93');
NONSEQUENCED VALID PERIOD '1993/03/15-1993/03/22'
 INSERT INTO insett VALUES ('11-esima93');
NONSEQUENCED VALID PERIOD '1993/03/22-1993/03/29'
 INSERT INTO insett VALUES ('12-esima93');


NONSEQUENCED VALID PERIOD '1993/01-1993/02' INSERT INTO inmesi VALUES ('gen93');
NONSEQUENCED VALID PERIOD '1993/02-1993/03' INSERT INTO inmesi VALUES ('feb93');
NONSEQUENCED VALID PERIOD '1993/03-1993/04' INSERT INTO inmesi VALUES ('mar93');
NONSEQUENCED VALID PERIOD '1993/04-1993/05' INSERT INTO inmesi VALUES ('apr93');
NONSEQUENCED VALID PERIOD '1993/05-1993/06' INSERT INTO inmesi VALUES ('mag93');
NONSEQUENCED VALID PERIOD '1993/06-1993/07' INSERT INTO inmesi VALUES ('giu93');
NONSEQUENCED VALID PERIOD '1993/07-1993/08' INSERT INTO inmesi VALUES ('lug93');
NONSEQUENCED VALID PERIOD '1993/08-1993/09' INSERT INTO inmesi VALUES ('ago93');
NONSEQUENCED VALID PERIOD '1993/09-1993/10' INSERT INTO inmesi VALUES ('set93');
NONSEQUENCED VALID PERIOD '1993/10-1993/11' INSERT INTO inmesi VALUES ('ott93');
NONSEQUENCED VALID PERIOD '1993/11-1993/12' INSERT INTO inmesi VALUES ('nov93');
NONSEQUENCED VALID PERIOD '1993/12-1994/01' INSERT INTO inmesi VALUES ('dic93');



VALID
(NONSEQUENCED VALID VALID(a2)
 SELECT a2.acctno, a2.acchol, a2.balance, a2.anintrate
 FROM (NONSEQUENCED VALID VALID(a0)
       SELECT a0.acctno, a0.acchol, a0.balance, a0.anintrate
       FROM (NONSEQUENCED VALID VALID(e2)
             SELECT e1.acctno, e1.acchol, e1.balance, e1.anintrate
             FROM upward AS e1, insett AS e2
             WHERE INTERVAL(INTERSECT(VALID(e1), VALID(e2)))
                   > INTERVAL '0' SECOND) AS a0,
            upward AS a1
       WHERE VALID(a1) CONTAINS VALID(a0)
         AND (a1.acctno=a0.acctno) AND (a1.acchol=a0.acchol)
         AND (a1.balance=a0.balance) AND (a1.anintrate=a0.anintrate)) AS a2
 WHERE EXISTS (SELECT *
               FROM (NONSEQUENCED VALID VALID(e4)
                     SELECT e3.acctno, e3.acchol, e3.balance, e3.anintrate
                     FROM upward AS e3, inmesi AS e4
                     WHERE INTERVAL(INTERSECT(VALID(e3), VALID(e4)))
                           > INTERVAL '0' SECOND) AS a3
               WHERE (a2.acctno=a3.acctno) AND (a2.acchol=a3.acchol)
                 AND (a2.balance=a3.balance) AND (a2.anintrate=a3.anintrate)
                 AND (VALID(a3) CONTAINS VALID(a2)
                  OR VALID(a3) + INTERVAL '604800' SECOND CONTAINS VALID(a2))))
(PERIOD);
