1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| -- Donne la date de début de période
CREATE VIEW Debut
AS SELECT Personne
, Date AS Debut
FROM MaTable AS T1
WHERE NOT EXISTS (SELECT 1
FROM MaTable AS T2
WHERE T1.Personne = T2.Personne
AND T1.Date = T2.Date - 1
)
;
-- Donne la date de fin de période
CREATE VIEW Fin
AS SELECT Personne
, Date AS Fin
FROM MaTable AS T1
WHERE NOT EXISTS (SELECT 1
FROM MaTable AS T2
WHERE T1.Personne = T2.Personne
AND T1.Date = T2.Date - 1
)
;
-- Donne un numéro d'ordre à la période
CREATE VIEW OrdreDebut
AS SELECT Personne
, Debut
, ( SELECT COUNT(*) + 1
FROM Debut AS D2
WHERE D1.Personne = D2.Personne
AND D1.Debut >= D2.Debut
) AS Ordre
FROM Debut AS D1
;
-- Donne un numéro d'ordre à la période
CREATE VIEW OrdreFin
AS SELECT Personne
, Fin
, ( SELECT COUNT(*) + 1
FROM Fin AS D2
WHERE D1.Personne = D2.Personne
AND D1.Fin >= D2.Fin
) AS Ordre
FROM Fin AS D1
;
-- Regroupe le tout
SELECT Personne
, Debut
, Fin
FROM OrdreDebut AS D
INNER JOIN OrdreFin AS F
ON D.Personne = F.Personne
AND D.Ordre = F.Ordre
; |
Partager