Sqlserver
 sql >> база данни >  >> RDS >> Sqlserver

Как да включите само редове, където следващият ред е в рамките на 12 часа и да класирате съответно

Примерни данни

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');

Заявка

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;

Резултат

+--------------------------------------+--------------------------------------+-------------------------+-------------------------+-------------------------+------------------+-----------+-----+-----------+
|               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 |
+--------------------------------------+--------------------------------------+-------------------------+-------------------------+-------------------------+------------------+-----------+-----+-----------+

Изпълнете заявката стъпка по стъпка, cte по cte и разгледайте междинните резултати, за да разберете как работи.

CTE_Prev връща PrevStart от предишния ред. Ако това е първият ред за пациент, той е NULL , затова го зададох на константа 2001-01-01 .

CTE_Markers връща GapIsLargeMarker зададено на 1, ако интервалът между два реда е повече от 12 часа. Отбелязва се с 1 тези редове, където започва нова "последователност".

CTE_Sequences попълва поредните номера SeqNumber използвайки текуща сума.

CTE_Ranks изчислява номера на редове (rnk ) във всяка последователност и колко реда (SeqLength ) са във всяка последователност.

Накрая връщаме само тези последователности, които имат повече от 1 ред в тях.



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Заявка за получаване на имената на всички таблици в базата данни на SQL Server 2008

  2. SqlDateTime.MinValue !=DateTime.MinValue, защо?

  3. SQL Server:класиране на тип данни в аритметични операции?

  4. Как да подходим към ETL мисия?

  5. Не може да се компилира SSIS компонент; Липсващо сглобяване във Visual Studio 2008