
Period Examples
***************

A period can be represented in Informix using two DATETIME columns.
Following are examples of a closed-closed representation of period
operations. 

CREATE TABLE perexample
{
    perstart     DATETIME  YEAR TO SECOND
    perend       DATETIME  YEAR TO SECOND
CHECK ( perstart < perend );
}

Creating indices

CREATE INDEX perstart ON perexample( perstart )
CREATE INDEX perend   ON perexample( perend )

Populating the database

INSERT INTO perexample [ perstart, perend ]
            { VALUES (DATETIME(1991-01-01 12:34:56) YEAR TO SECOND,
                      DATETIME(1994-04-03 10:01:23) YEAR TO SECOND )
            }
INSERT INTO perexample [ perstart, perend ]
            { VALUES (DATETIME(1995-07-10 08:54:05) YEAR TO SECOND),
                      DATETIME(1997-11-22 18:30:14) YEAR TO SECOND) )
            }

Predicates
**********

    

These are the predicates which can be used in the period operations. Two periods p and q
are used here in each example. p is obtained by means of a SELECT statement on the 
database.
  q is [ DATETIME(1994-01-03 03:05:32) YEAR TO SECOND , 
         DATETIME(1996-10-23 05:45:56) YEAR TO SECOND ]

All the queries return the period [1991-01-01 12:34:56, 1994-04-03 10:01:23 ]

63. SELECT *
    FROM   perexample
    WHERE  perstart = (DATETIME(1991-01-01 12:34:56) YEAR TO SECOND
    AND    perend   = DATETIME(1994-04-03 10:01:23) YEAR TO SECOND


64. Following will give periods before ["1994-11-03 3:05:32", "1996-12-09 23:11:44"]
    SELECT *
    FROM   perexample
    WHERE  perend + INTERVAL(1) DAY TO DAY  < DATETIME(1994-11-03 03:05:32)
                           YEAR  TO SECOND )
    Here a granularity of one day is assumed.

65. Following retrieves periods by performing inverse of before operation 
between each of the records and ["1989-11-03 3:05:32", "1990-12-09 23:11:44"]

    SELECT *
    FROM   perexample
    WHERE  DATETIME(1990-12-09 23:11:44) YEAR TO SECOND + INTERVAL(1) DAY TO DAY  < 
            perstart


66. SELECT *
    FROM   perexample
    WHERE  perend + INTERVAL(1) DAY TO DAY  = DATETIME(1994-04-04 10:01:23) YEAR TO SECOND 
                                           


67. SELECT *
    FROM   perexample
    WHERE  DATETIME(1990-12-31 12:34:56) YEAR TO SECOND + INTERVAL(1) DAY TO DAY  = 
           perstart 

68. q = [ "1992-09-08 03:34:32", "1995-11-03 05:32:44" ]

    SELECT *
    FROM   perexample
    WHERE  perstart <= DATETIME(1995-11-03 05:32:44) YEAR TO SECOND 
    AND    DATETIME(1992-09-08 03:34:32) YEAR TO SECOND  <=  perend


69. q = [ "1977-07-06 07:10:43", "1992-09-08 03:34:32" ]
 
    SELECT *
    FROM   perexample
    WHERE  DATETIME("1977-07-06 07:10:43", YEAR TO SECOND) <= perstart
    AND    perend <= DATETIME(1992-09-08 03:34:32) YEAR TO SECOND 

70. q = [ "1977-07-06 07:10:43", "1995-11-03 05:32:44" ]
   
    SELECT *
    FROM   perexample
    WHERE  DATETIME(1995-11-03 05:32:44) YEAR TO SECOND < perstart
    AND	   perend <  DATETIME(1977-07-06 07:10:43) YEAR TO SECOND

71. q = [ "1993-07-06 07:10:43", "1993-11-03 05:32:44" ]

    SELECT *
    FROM   perexample
    WHERE  perstart < DATETIME(1993-07-06 07:10:43) YEAR TO SECOND
    AND    DATETIME(1993-11-03 05:32:44) YEAR TO SECOND 

72.  q = [ "1991-01-01 12:34:56", "1995-11-03 05:32:44" ]

    SELECT *
    FROM   perexample
    WHERE  perstart =  DATETIME(1991-01-01 12:34:56) YEAR TO SECOND
    AND    perend < DATETIME(1995-11-03 05:32:44) YEAR TO SECOND 

73. q = ["1991-01-01 12:34:56", "1993-07-06 07:10:43" ]

   
    SELECT *
    FROM   perexample
    WHERE  DATETIME(1991-01-01 12:34:56) YEAR TO SECOND = perstart
    AND    DATETIME(1993-07-06 07:10:43) YEAR TO SECOND  < perend

74. q =  [ "1977-07-06 07:10:43", "1994-04-03 10:01:23" ]

    SELECT *
    FROM   perexample
    WHERE  DATETIME(1977-07-06 07:10:43) YEAR TO SECOND  < perstart
    AND    perend = DATETIME(1994-04-03 10:01:23) YEAR TO SECOND 

75.  q = [  "1993-07-06 07:10:43", "1994-04-03 10:01:23" ]

    SELECT *
    FROM   perexample
    WHERE  perstart < DATETIME( 1993-07-06 07:10:43 ) YEAR TO SECOND 
    AND    perend  = DATETIME( 1994-04-03 10:01:23 ) YEAR TO SECOND 


INSERT INTO perexample [ perstart, perend ]
            { VALUES ( NULL, NULL ) }
           
77.  SELECT *
     FROM   perexample
     WHERE perstart IS NULL
           perend   IS NULL


Datetime constructors
*********************

 For Period  p = [  DATETIME( 1991-01-01 12:34:56 ) YEAR TO SECOND,
                       DATETIME (1994-04-03 10:01:23 ) YEAR TO SECOND ]

78. beginning(p) = DATETIME(1991-01-01 12:34:56) YEAR TO SECOND 

79. previous(p)  = DATETIME(1991-01-01 12:34:56) YEAR TO SECOND
                     - INTERVAL(1) DAY TO DAY 

80. last(p)      = DATETIME(1994-04-03 10:01:23) YEAR TO SECOND

81. ending(p)    = DATETIME(1994-04-03 10:01:23 YEAR TO SECOND 
                     + INTERVAL(1) DAY TO DAY 

82. duration(p)  =  DATETIME(1994-04-03 10:01:23) YEAR TO SECOND
                      - DATETIME(1991-01-01 12:34:56) YEAR TO SECOND
                      + INTERVAL(1) DAY TO DAY


84. SELECT *
    FROM   perexample
    WHERE  perstart = DATETIME(1990-23-12 12:34:56) YEAR TO SECOND  +
                      INTERVAL(9) DAY TO DAY 
    AND    perend   = DATETIME(1994-03-25 10:01:23) YEAR TO SECOND  +
                      INTERVAL(9) DAY TO DAY 


86. SELECT *
    FROM   perexample
    WHERE  perstart = DATETIME(1991-01-01 12:38:56) YEAR TO SECOND - 
                      INTERVAL(5) MINUTE TO MINUTE 
    AND    perend   = DATETIME(1994-04-03 10:05:23) YEAR TO SECOND +
                      INTERVAL(5) MINUTE TO MINUTE 

    a =  "1990-02-03 10:45:06"
    b =  "1993-08-07 03:22:54" 
    
87. a extend b is given by

    INSERT INTO perexample [ perstart, perend ]
            { VALUES (DATETIME(1990-02-03 10:45:06) YEAR TO SECOND,
                      DATETIME(1993-08-07 03:22:54) YEAR TO SECOND )
            }

              
let,
    p = ["1991-01-01 12:34:56", "1994-04-03 10:01:23" ]
    q = ["1995-07-10 08:54:05", "1997-11-22 18:30:14" ]

88. p extend q is given by
    
  [  MIN( DATETIME(1991-01-01 12:34:56) YEAR TO SECOND,
          DATETIME(1995-07-10 08:54:05) YEAR TO SECOND),
     MAX( DATETIME(1994-04-03 10:01:23) YEAR TO SECOND,
          DATETIME(1997-11-22 18:30:14) YEAR TO SECOND)
  ]

89. p extend a is given by

 [  MIN( DATETIME(1991-01-01 12:34:56) YEAR TO SECOND,
         DATETIME(1990-02-03 10:45:06) YEAR TO SECOND),
    MAX( DATETIME(1994-04-03 10:01:23) YEAR TO SECOND,
         DATETIME(1993-08-07 03:22:54) YEAR TO SECOND)
 ]


90. a extend p is given by
 
  [  MIN( DATETIME(1991-01-01 12:34:56) YEAR TO SECOND,
          DATETIME(1990-02-03 10:45:06) YEAR TO SECOND),
    MAX( DATETIME(1994-04-03 10:01:23) YEAR TO SECOND,
         DATETIME(1993-08-07 03:22:54) YEAR TO SECOND)
  ]    
   

91. p intersection q is

  [  MAX( DATETIME(1991-01-01 12:34:56) YEAR TO SECOND,
          DATETIME(1995-07-10 08:54:05) YEAR TO SECOND),
     MIN( DATETIME(1994-04-03 10:01:23) YEAR TO SECOND,
          DATETIME(1997-11-22 18:30:14) YEAR TO SECOND)
  ]

92. 

 For 
    p = ["1991-01-01 12:34:56", "1994-04-03 10:01:23" ]
    q = ["1993-07-10 08:54:05", "1997-11-22 18:30:14" ]

since p overlaps q
p - q is given by

  [ DATETIME(1991-01-01 12:34:56) YEAR TO SECOND , 
    DATETIME(1993-07-10 08:45:05) YEAR TO SECOND 
  ]

93. p union q

[  MIN( DATETIME(1991-01-01 12:34:56) YEAR TO SECOND,
        DATETIME(1995-07-10 08:54:05) YEAR TO SECOND),
   MAX( DATETIME(1994-04-03 10:01:23) YEAR TO SECOND,
        DATETIME(1997-11-22 18:30:14) YEAR TO SECOND)
]



96. if a = DATETIME(5 04:44:03) DAY TO SECOND 
    then assuming granularity of a day, the period becomes

    [DATETIME(5 04:44:03) DAY TO SECOND,
     DATETIME(5 04:44:03) DAY TO SECOND + INTERVAL(1) DAY TO DAY ]

    [DATETIME(5 04:44:03) DAY TO SECOND,
     DATETIME(6 04:44:03) DAY TO SECOND ]







   




