按行之间的差异分组 SQL?

Grouping SQL by differences between rows?

我取了几个数据点的最小值和最大值,但其中一些数据点让患者在不同的日子去到相同的地点。有没有办法添加一个索引或代码,根据语句或类似的顺序将它们区分为单独的组?我想将它们区分为不同的组(通过分离患者离开一个位置,去一个新的位置,然后回到原来的位置(在分组方面将其视为一个新的位置)?

这是制作虚拟数据的代码 table。

    CREATE TABLE #StartandStop (
     [hospitalname] varchar(255),
     pt_id varchar(255),
     [Location] varchar(255),
     startdatetime datetime,
     enddatetime datetime
     ); 

     INSERT INTO #StartandStop ([hospitalname],[pt_id],[Location],[startdatetime],[enddatetime])
     VALUES ('ClevelandClinic', '1','A','01-01-2020 10:35', '01-02-2020 12:30'),
     ('ClevelandClinic', '1','B','01-02-2020 12:30', '01-03-2020 00:00'),
     ('ClevelandClinic', '1','B','01-03-2020 00:00', '01-04-2020 00:00'),
     ('ClevelandClinic', '1','B','01-04-2020 00:00', '01-04-2020 19:30'),
     ('ClevelandClinic', '1','C','01-04-2020 19:30', '01-05-2020 18:44'),
     ('ClevelandClinic', '1','B','01-05-2020 18:44', '01-05-2020 07:09'),
     ('ClevelandClinic', '1','D','01-05-2020 07:09', '01-05-2020 22:22'),
     ('ClevelandClinic', '1','D','01-05-2020 22:22', '01-06-2020 00:00'),
     ('ClevelandClinic', '1','D','01-06-2020 00:00', '01-07-2020 00:00'),
     ('ClevelandClinic', '1','D','01-07-2020 00:00', '01-08-2020 00:00'),
     ('ClevelandClinic', '1','C','01-08-2020 00:00', '01-08-2020 20:02');

这是获取结果的代码。

 select hospitalname,pt_id,location,min(startdatetime) as 'Start',max(enddatetime) as 'End'
  from  #StartandStop
 group by hospitalname,pt_id,location

这些是我在不考虑患者在单元之间来回移动的情况下,只取每个单元的最小开始和最大停止时得到的结果。

   hospitalname        pt_id    location    Start                       End
   ClevelandClinic     1         A          2020-01-01 10:35:00.000     2020-01-02 12:30:00.000
   ClevelandClinic     1         B          2020-01-02 12:30:00.000     2020-01-05 07:09:00.000
   ClevelandClinic     1         C          2020-01-04 19:30:00.000     2020-01-08 20:02:00.000
   ClevelandClinic     1         D          2020-01-05 07:09:00.000     2020-01-08 00:00:00.000

这些是我想要得到的结果:

 Hospital        Patient     Location     Start                          Stop
 ClevelandClinic    1        A            2020-01-01 10:35:00.000        2020-01-02 12:30:00.000
 ClevelandClinic    1        B            2020-01-02 12:30:00.000        2020-01-04 19:30:00.000
 ClevelandClinic    1        C            2020-01-04 19:30:00.000        2020-01-05 18:44:00.000
 ClevelandClinic    1        B            2020-01-05 18:44:00.000        2020-01-05 07:09:00.000
 ClevelandClinic    1        D            2020-01-05 07:09:00.000        2020-01-08 00:00:00.000
 ClevelandClinic    1        C            2020-01-08 00:00:00.000        2020-01-08 20:02:00.000

请告知是否有办法将对同一位置的第一次和第二次访问标记或区分为不同。谢谢!

ALTER TABLE #StartandStop ADD color INT

DECLARE @color INT
SELECT @color = 0

UPDATE ss SET color = @color, @color = CASE WHEN s.Location <> s.LastLocation THEN @color + 1 ELSE @color END
FROM #StartandStop ss JOIN (SELECT hospitalname, pt_id, Location, LAG(location) OVER (ORDER BY hospitalname,pt_id,startdatetime,location) AS LastLocation, startdatetime, enddatetime 
    FROM #StartandStop) s ON (ss.hospitalname = s.hospitalname AND ss.pt_id = s.pt_id AND ss.location = s.location AND ss.startdatetime = s.startdatetime AND ss.enddatetime = s.enddatetime)

SELECT hospitalname AS Hospital, pt_id AS Patient, MIN(Location) AS Location, MIN(startdatetime) AS Start, MAX(enddatetime) AS Stop
FROM #StartandStop
GROUP BY hospitalname,pt_id,color
ORDER BY 1,2,4,3