I wanted to manipulate the end date to next record startdate or records that happened on same day for shopID and then grab the starttime of later record and update the value in the prior row endtime if the starttime prior to next row start time and end date crosses the next record start date.
For example (ShopId: 9856) end date of first record is '2020-01-08 09:18:52' which is crosses the start time of next record '2020-01-08 09:09:48'. Here my logic need to look for such instances happen on same day and manipulate the enddate of first record to next record startdate
CREATE TABLE [dbo].[TestTab1](
StoreID [int] NOT NULL,
PurchaseID [int] NOT NULL,
ShopID [int] NOT NULL,
LocationID [int] NOT NULL,
starttime [datetime] NOT NULL,
Endtime [datetime] NOT NULL,
) ON [PRIMARY]
INSERT INTO [TestTab1]
VALUES
(1020,20200102,9856,0010,'2020-01-08T09:08:53','2020-01-08T09:18:52'),
(1021,20200102,9856,0020,'2020-01-08T09:09:48','2020-01-08T09:11:52'),
(1022,20200102,9856,0030,'2020-01-08T09:12:53','2020-01-08T09:14:52'),
(1023,20200102,9856,0040,'2020-01-08T09:16:48','2020-01-08T09:18:52')
Final Result set:
StoreID |PurchaseID |ShopID |LocationID |starttime |Endtime
--------|-----------|-------|-----------|--------------------|-------------------
1020 |20200102 |9856 |10 |2020-01-08 09:08:53 |2020-01-08 09:09:48
1021 |20200103 |9856 |20 |2020-01-08 09:09:48 |2020-01-08 09:11:52
1022 |20200102 |9856 |30 |2020-01-08 09:12:53 |2020-01-08 09:14:52
1023 |20200104 |9856 |40 |2020-01-08 09:16:48 |2020-01-08 09:18:52