HI,
I am trying lot to get this query right, can you please help?
Input data:
Expected output:
I am trying to get all actions taken place between actions TEST1/TEST2 (this is tracker report which identify how much time user took to do certain actions, start or end can be TEST1 or TEST2), different scenarios in the sample:
Between ID = 1 and 4 there are two actions (action1 and action2) happened (between TEST1/TEST2, basically there will be two specific actions which determine the start or end) and it should be reported with datetime of start of the activity (id = 1 in this case)
Between ID = 4 and 5 there are no actions happened so nothing to report
Between ID = 5 and 7 (next day start of the activity) there is Action1 happened so it should be reported with datetime from ID = 5
Between ID = 7 and 8 (next day start of the activity) there are no actions happened so nothing to report
Between ID = 8 and 11 there are two actions Action1/2 happened so it should be reported with datetime from ID = 8
ID = 11 is last in the data and nothing to report
Repeats for different userid's
Hope it is clear, Query as below:
DECLARE @INPUT TABLE
(
ID INT,
USERID INT,
CUSTOMERID INT,
ACTIONATE DATETIME,
ACTION VARCHAR(10),
COMMENTS VARCHAR(100)
)
INSERT INTO @INPUT
SELECT '1','1','1','2021-01-01 10:30:01','TEST1','Start' UNION ALL
SELECT '2','1','2','2021-01-01 10:31:01','ACTION1','' UNION ALL
SELECT '3','1','1','2021-01-01 10:31:16','ACTION2','' UNION ALL
SELECT '4','1','3','2021-01-01 10:32:02','TEST2','End/Start' UNION ALL
SELECT '5','1','1','2021-01-01 11:32:02','TEST1','End/Start' UNION ALL
SELECT '6','1','3','2021-01-01 11:34:02','ACTION1','' UNION ALL
SELECT '7','1','2','2021-01-02 10:44:02','TEST1','Start of new day' UNION ALL
SELECT '8','1','1','2021-01-03 10:30:01','TEST1','Start of new day' UNION ALL
SELECT '9','1','2','2021-01-03 10:31:01','ACTION1','' UNION ALL
SELECT '10','1','1','2021-01-03 10:31:16','ACTION2','' UNION ALL
SELECT '11','1','3','2021-01-03 10:32:02','TEST2','End/Start' UNION ALL
SELECT '12','2','1','2021-01-01 9:30:01','TEST1','Start' UNION ALL
SELECT '13','2','2','2021-01-01 9:31:01','ACTION1','' UNION ALL
SELECT '14','2','1','2021-01-01 9:31:16','ACTION2','' UNION ALL
SELECT '15','2','1','2021-01-01 9:31:16','ACTION3','' UNION ALL
SELECT '16','2','3','2021-01-01 9:32:02','TEST2','End/Start' UNION ALL
SELECT '17','2','1','2021-01-01 9:40:02','TEST1','End/Start' UNION ALL
SELECT '18','2','3','2021-01-01 9:41:02','ACTION1','' UNION ALL
SELECT '19','2','2','2021-01-02 8:44:02','TEST1','Start of new day' UNION ALL
SELECT '20','2','1','2021-01-03 8:30:01','TEST1','Start of new day' UNION ALL
SELECT '21','2','2','2021-01-03 8:31:01','ACTION1','' UNION ALL
SELECT '22','2','1','2021-01-03 8:31:16','ACTION2','' UNION ALL
SELECT '23','2','3','2021-01-03 8:32:02','TEST2','End/Start'
SELECT *FROM @INPUT
DECLARE @OUTPUT TABLE
(
USERID INT,
ACTIONATE DATETIME,
ACTION VARCHAR(10)
)
INSERT INTO @OUTPUT
SELECT '1','2021-01-01 10:30:01','ACTION1' UNION ALL
SELECT '1','2021-01-01 10:30:01','ACTION2' UNION ALL
SELECT '1','2021-01-01 11:32:02','ACTION1' UNION ALL
SELECT '1','2021-01-03 10:30:01','ACTION1' UNION ALL
SELECT '1','2021-01-03 10:30:01','ACTION2' UNION ALL
SELECT '2','2021-01-01 9:30:01','ACTION1' UNION ALL
SELECT '2','2021-01-01 9:30:01','ACTION2' UNION ALL
SELECT '2','2021-01-01 9:30:01','ACTION3' UNION ALL
SELECT '2','2021-01-01 9:40:02','ACTION1' UNION ALL
SELECT '2','2021-01-03 8:30:01','ACTION1' UNION ALL
SELECT '2','2021-01-03 8:30:01','ACTION2'
SELECT *FROM @OUTPUT