Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Comment inclure uniquement les lignes où la ligne suivante est dans les 12 heures et classer en conséquence

Exemples de données

declare @table as table
(
    [CaseRef] uniqueidentifier,
    [PatientRef] uniqueidentifier,
    [StartDate] datetime,
    [FinishDate] datetime
);

insert into @table
(
    [CaseRef],
    [PatientRef],
    [StartDate],
    [FinishDate]
)
values
('DB79C49E-938C-4C40-B48E-3389D9339759', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 00:22:41', '2017-01-30 00:28:06'),
('4BFA4E3B-D313-4777-A290-3C13601D5C95', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 22:00:46', '2017-01-30 22:10:24'),
('F910D4DE-3CEE-4429-8844-DDE860D08192', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 22:25:49', '2017-01-30 22:27:58'),
('DF28DC91-02E3-47F2-88E0-397C2CBCFE41', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 22:44:11', '2017-01-30 22:53:22'),
('D6964286-8AE7-46AB-8DA5-88A347015C4D', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 22:55:17', '2017-01-30 23:01:57'),
('660B2ED7-B715-4A6C-A92B-D80267C0E4F5', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 23:06:16', '2017-01-30 23:08:28'),
('903AC539-4BB1-44AB-AFDB-D86C13310011', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 23:15:21', '2017-01-30 23:16:02'),
('75B88E5F-4795-4A21-9EA6-3B41CE958250', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 23:28:31', '2017-01-30 23:29:53'),
('DD6A4BD5-EF75-44CE-9309-4C14B2A21FF4', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 23:45:42', '2017-01-30 23:46:13'),
('518319BA-0EDE-46D8-B0B7-E8CEB233DEDF', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-30 23:54:02', '2017-01-31 00:03:13'),
('FB5A5A54-E580-40F2-94FD-64E20EA5C4DD', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-31 16:13:01', '2017-01-31 16:21:02'),
('8A4FD0C3-59BF-43AB-A829-F2396D6FB26A', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-01-31 18:26:14', '2017-01-31 18:39:20'),
('8CB94AF1-9664-4081-A2E1-271ED16B147B', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-02-01 08:10:41', '2017-02-01 08:18:18'),
('0DC6B68B-0458-48DF-B286-C1A978653981', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-02-01 15:40:45', '2017-02-01 15:48:24'),
('DB239857-6870-4AD9-8149-69ED6151CCB2', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-02-02 16:54:40', '2017-02-02 17:10:27'),
('938CCFF4-66C9-48B1-BDB7-D9144D2BD522', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-02-02 19:29:18', '2017-02-02 19:30:14'),
('1EC730D0-AF85-45BF-BD06-12B23124151F', 'A3E14866-4DD5-4001-AF63-21819F49B401', '2017-02-02 19:43:28', '2017-02-02 19:47:12');

Requête

WITH
CTE_Prev
AS
(
    SELECT
        CaseRef
        ,PatientRef
        ,StartDate
        ,FinishDate
        ,ISNULL(LAG(StartDate) OVER (PARTITION BY PatientRef ORDER BY StartDate),
            '2000-01-01') AS PrevStart
    FROM @Table AS T
)
,CTE_Markers
AS
(
    SELECT
        CaseRef
        ,PatientRef
        ,StartDate
        ,FinishDate
        ,PrevStart
        ,CASE WHEN (DATEDIFF(hour, PrevStart, StartDate) >= 12) 
            THEN 1 ELSE 0 END AS GapIsLargeMarker
    FROM CTE_Prev
)
,CTE_Sequences
AS
(
    SELECT
        CaseRef
        ,PatientRef
        ,StartDate
        ,FinishDate
        ,PrevStart
        ,GapIsLargeMarker
        ,SUM(GapIsLargeMarker) OVER (PARTITION BY PatientRef ORDER BY StartDate 
            ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS SeqNumber
    FROM CTE_Markers
)
,CTE_Ranks
AS
(
    SELECT
        CaseRef
        ,PatientRef
        ,StartDate
        ,FinishDate
        ,PrevStart
        ,GapIsLargeMarker
        ,SeqNumber
        ,ROW_NUMBER() OVER (PARTITION BY PatientRef,SeqNumber ORDER BY StartDate) AS rnk
        ,COUNT(*) OVER (PARTITION BY PatientRef, SeqNumber) AS SeqLength
    FROM CTE_Sequences
)
SELECT
    CaseRef
    ,PatientRef
    ,StartDate
    ,FinishDate
    ,PrevStart
    ,GapIsLargeMarker
    ,SeqNumber
    ,rnk
    ,SeqLength
FROM CTE_Ranks
WHERE SeqLength > 1
ORDER BY PatientRef, StartDate;

Résultat

+--------------------------------------+--------------------------------------+-------------------------+-------------------------+-------------------------+------------------+-----------+-----+-----------+
|               CaseRef                |              PatientRef              |        StartDate        |       FinishDate        |        PrevStart        | GapIsLargeMarker | SeqNumber | rnk | SeqLength |
+--------------------------------------+--------------------------------------+-------------------------+-------------------------+-------------------------+------------------+-----------+-----+-----------+
| 4BFA4E3B-D313-4777-A290-3C13601D5C95 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 22:00:46.000 | 2017-01-30 22:10:24.000 | 2017-01-30 00:22:41.000 |                1 |         2 |   1 |         9 |
| F910D4DE-3CEE-4429-8844-DDE860D08192 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 22:25:49.000 | 2017-01-30 22:27:58.000 | 2017-01-30 22:00:46.000 |                0 |         2 |   2 |         9 |
| DF28DC91-02E3-47F2-88E0-397C2CBCFE41 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 22:44:11.000 | 2017-01-30 22:53:22.000 | 2017-01-30 22:25:49.000 |                0 |         2 |   3 |         9 |
| D6964286-8AE7-46AB-8DA5-88A347015C4D | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 22:55:17.000 | 2017-01-30 23:01:57.000 | 2017-01-30 22:44:11.000 |                0 |         2 |   4 |         9 |
| 660B2ED7-B715-4A6C-A92B-D80267C0E4F5 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 23:06:16.000 | 2017-01-30 23:08:28.000 | 2017-01-30 22:55:17.000 |                0 |         2 |   5 |         9 |
| 903AC539-4BB1-44AB-AFDB-D86C13310011 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 23:15:21.000 | 2017-01-30 23:16:02.000 | 2017-01-30 23:06:16.000 |                0 |         2 |   6 |         9 |
| 75B88E5F-4795-4A21-9EA6-3B41CE958250 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 23:28:31.000 | 2017-01-30 23:29:53.000 | 2017-01-30 23:15:21.000 |                0 |         2 |   7 |         9 |
| DD6A4BD5-EF75-44CE-9309-4C14B2A21FF4 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 23:45:42.000 | 2017-01-30 23:46:13.000 | 2017-01-30 23:28:31.000 |                0 |         2 |   8 |         9 |
| 518319BA-0EDE-46D8-B0B7-E8CEB233DEDF | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-30 23:54:02.000 | 2017-01-31 00:03:13.000 | 2017-01-30 23:45:42.000 |                0 |         2 |   9 |         9 |
| FB5A5A54-E580-40F2-94FD-64E20EA5C4DD | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-31 16:13:01.000 | 2017-01-31 16:21:02.000 | 2017-01-30 23:54:02.000 |                1 |         3 |   1 |         2 |
| 8A4FD0C3-59BF-43AB-A829-F2396D6FB26A | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-01-31 18:26:14.000 | 2017-01-31 18:39:20.000 | 2017-01-31 16:13:01.000 |                0 |         3 |   2 |         2 |
| 8CB94AF1-9664-4081-A2E1-271ED16B147B | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-02-01 08:10:41.000 | 2017-02-01 08:18:18.000 | 2017-01-31 18:26:14.000 |                1 |         4 |   1 |         2 |
| 0DC6B68B-0458-48DF-B286-C1A978653981 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-02-01 15:40:45.000 | 2017-02-01 15:48:24.000 | 2017-02-01 08:10:41.000 |                0 |         4 |   2 |         2 |
| DB239857-6870-4AD9-8149-69ED6151CCB2 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-02-02 16:54:40.000 | 2017-02-02 17:10:27.000 | 2017-02-01 15:40:45.000 |                1 |         5 |   1 |         3 |
| 938CCFF4-66C9-48B1-BDB7-D9144D2BD522 | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-02-02 19:29:18.000 | 2017-02-02 19:30:14.000 | 2017-02-02 16:54:40.000 |                0 |         5 |   2 |         3 |
| 1EC730D0-AF85-45BF-BD06-12B23124151F | A3E14866-4DD5-4001-AF63-21819F49B401 | 2017-02-02 19:43:28.000 | 2017-02-02 19:47:12.000 | 2017-02-02 19:29:18.000 |                0 |         5 |   3 |         3 |
+--------------------------------------+--------------------------------------+-------------------------+-------------------------+-------------------------+------------------+-----------+-----+-----------+

Exécutez la requête étape par étape, côté par côté et examinez les résultats intermédiaires pour comprendre comment cela fonctionne.

CTE_Prev renvoie PrevStart de la rangée précédente. S'il s'agit de la première ligne d'un patient, c'est NULL , donc je l'ai mis à une constante 2001-01-01 .

CTE_Markers renvoie GapIsLargeMarker mis à 1 si l'écart entre deux lignes est supérieur à 12 heures. Il marque avec 1 ces lignes où une nouvelle "séquence" commence.

CTE_Sequences remplit les numéros de séquence SeqNumber en utilisant le total cumulé.

CTE_Ranks calcule les numéros de lignes (rnk ) dans chaque séquence et combien de lignes (SeqLength ) sont dans chaque séquence.

Enfin, nous renvoyons uniquement les séquences contenant plus d'une ligne.