Fonction de test de chevauchement d’intervalles

Voici plusieurs formes d’écriture d’une fonction de chevauchement d’intervalles pour MS SQL Server.

Tester le chevauchement des intervalles permet de savoir par exemple si deux périodes se recoupent. C’est parfois indispensable dans la gestion des emplois du temps pour empêcher qu’une même personne ne doive être présente à deux endroits différents en même temps ou qu’un même lieu soit occupé au même moment…
Il existe une fonction SQL prévue pour cela de non OVERLAPS dans la norme SQL. Cependant l’écriture de cette fonction peut influencer grandement les performances. C’est pourquoi nous allons voir différentes formes d’une telle écriture.

ATTENTION : ne sont pas considérés comme se chevauchant un intervalle qui commence ou le précédent fini (égalité des bornes « fin » du précédent et « début » du suivant). En effet, par principe les intervalles sont considérés comme ayant tous des bornes fermées à gauche et ouvertes à droite [—[ ou bien ouvertes à gauche et fermées à droite ]—].

Intervalles se chevauchant
Intervalles se chevauchant
Intervalles sans chevauchement
Intervalles sans chevauchement

0 РLa formulation du pr̩dicat normatif OVERLAPS est la suivante :

 ::=  
 ::=  
 ::= OVERLAPS  
 ::=  
 ::=

General Rules
1) If the value of row value predicand 1 is the null value or the value of row value predicand 2 is the
null value, then the result of the overlaps predicate is Unknown and no further General Rules of this
Subclause are applied.
2) Let D1 be the value of the first field of row value predicand 1 and D2 be the value of the first field of
row value predicand 2.
3) Case:
a) If the most specific type of the second field of row value predicand 1 is a datetime data type, then
let E1 be the value of the second field of row value predicand 1.
b) If the most specific type of the second field of row value predicand 1 is INTERVAL, then let I1 be
the value of the second field of row value predicand 1. Let E1 = D1 + I1.
4) If D1 is the null value or if E1 inf D1, then let S1 = E1 and let T1 = D1. Otherwise, let S1 = D1 and let T1
= E1.
5) Case:
a) If the most specific type of the second field of row value predicand 2 is a datetime data type, then
let E2 be the value of the second field of row value predicand 2.
b) If the most specific type of the second field of row value predicand 2 is INTERVAL, then let I2 be
the value of the second field of row value predicand 2. Let E2 = D2 + I2.
6) If D2 is the null value or if E2 inf D2, then let S2 = E2 and let T2 = D2. Otherwise, let S2 = D2 and let T2
= E2.
7) The result of the overlaps predicate is the result of the following expression:

( S1 > S2 AND NOT ( S1 >= T2 AND T1 >= T2 ) )
OR
( S2 > S1 AND NOT ( S2 >= T1 AND T2 >= T1 ) )
OR
( S1 = S2 AND ( T1  T2 OR T1 = T2 ) )

NOTA : le terme inf étant le caractère :
<

Cette définition inclue une problématique qui est qu'un intervalle peut avoir une borne indéfinie, par exemple venir d'un passé lointain, comme de ne pas savoir quand il va se terminer et dans les deux cas, l'une des bornes peut être NULL (voire les deux).

Intervalles avec borne ouverte se chevauchant
Intervalles avec borne ouverte se chevauchant
Intervalles avec borne ouverte sans chevauchement
Intervalles avec borne ouverte sans chevauchement

1 – test de chevauchement pour MS SQL Server :


CREATE FUNCTION dbo.f_overlaps (@i1_begin DATETIME2(0), @i1_end   DATETIME2(0),
                                @i2_begin DATETIME2(0), @i2_end   DATETIME2(0))
RETURNS BIT
AS
BEGIN
/* Nous suposons qu'aucun défaut de coordination de temps n'est possible (contrainte CHECK dans la table). Si ce n'est pas le cas :
   IF @i1_begin > @i1_end RETURN NULL;
   IF @i2_begin > @i2_end RETURN NULL;
*/    
-- pas de chevauchement si l'un des intervalle est avant ou après l'autre :
   IF @i2_begin >= @i1_end RETURN 0;
   IF @i2_end <= @i1_begin RETURN 0;
-- sinon, chevauchement :
   RETURN 1;
END;

--------
Frédéric Brouard, SQLpro - ARCHITECTE DE DONNÉES, http://sqlpro.developpez.com/
Expert bases de données relationnelles et langage SQL. MVP Microsoft SQL Server
www.sqlspot.com : modélisation, conseil, audit, optimisation, tuning, formation
* * * * *  Enseignant CNAM PACA - ISEN Toulon - CESI Aix en Provence  * * * * *

MVP Microsoft SQL Server

Laisser un commentaire