Skip to content

Check overlapping valid times

If you have a bi-temporal entity in your model, it is advisable to check for overlapping valid timelines. See Allen's interval algebra for more information.

A timeline doesn't overlap if:

  • Condition 1. Timeline X is completely after timeline Y
                     x.valid_from            x.valid_to
                           |-----Timeline X-----|

y.valid_from         y.valid_to
   |-----Timeline Y-----|
  • Condition 2. Timeline X is completely before timeline Y
x.valid_from         x.valid_to
   |-----Timeline X-----|

                  y.valid_from               y.valid_to
                           |-----Timeline Y-----| 

An overlap exists if neither condition 1 nor 2 is true: if one range is neither completely after the other nor completely before the other, then they must overlap. So:

  • NOT(condition 1 OR condition 2)

According to De Morgan's law, this is equal to:

  • NOT(condition 1) AND NOT(condition 2)


The following query returns all records in part retail price temporal that violates the set constraint.

WITH QRY AS 
    ( SELECT 
             [x].[nbr] AS nbr
           , [x].[valid_from] AS valid_from
           , [x].[acm_rownumber] AS acm_rownumber
        FROM [tpc_h].[part_retail_price_temporal] AS [x] 
        JOIN [tpc_h].[part_retail_price_temporal] AS [y] 
        ON   [x].[nbr]=[y].[nbr]
        AND  [x].[valid_from] != [y].[valid_from]
        AND  [x].[valid_from] < [y].[valid_to]
        AND  [x].[valid_to] > [y].[valid_from]
    )

The query:

  • joins the table with itself: x and y on the primary key nbr
  • it returns all records where:
    • x.valid_from is not equal to y.valid_from.
    • x.valid_from is earlier than y.valid_to. If this is the case, a timeline overlaps (violation of condition 1).
    • x.valid_to is later than y.valid_from. If this is the case, a timeline overlaps (violation of condition 2).