Quantcast
Channel: Latest Questions by aRookieBIdev
Viewing all articles
Browse latest Browse all 76

SQL Query to group relatively closer dates

$
0
0

I am having a tough time with grouping records based on the closer dates and one other condition

In the data shown in the picture ,

I would like to group ProcessId 3 , 4 AND 5 together as one group based on the following condition

  • Process 4 started with in 2 days after the process 3 ended.
  • Process 5 started with in 2 days after the process 4 ended.
  • All process has less than 10 steps. (Any process more than 10 steps should not be grouped)

Any process more than 10 steps will break the continuity of the group. There should be new group after that

Thanks you and appreciate any help

CREATE TABLE [dbo].[process] 
  ( 
     [client] [VARCHAR](50) NULL,[processid] [VARCHAR](50) NULL, 
     [startdate] [DATETIME] NULL,[enddate] [DATETIME] NULL,[steps] [INT] NULL, 
     [groupprocessid] [INT] NULL 
  ); 


INSERT INTO [dbo].[process] 
            ([client],[processid],[startdate],[enddate],[steps],[groupprocessid] 
) 
VALUES      ('A',1,'2018-05-01 15:48:21.837','2018-05-02 15:48:21.837',5,NULL) 

INSERT INTO [dbo].[process] 
            ([client],[processid],[startdate],[enddate],[steps],[groupprocessid] 
) 
VALUES      ('A',2,'2018-05-03 15:48:21.837','2018-05-04 15:48:21.837',11,NULL) 

INSERT INTO [dbo].[process] 
            ([client],[processid],[startdate],[enddate],[steps],[groupprocessid] 
) 
VALUES      ('A',3,'2018-05-05 15:48:21.837','2018-05-06 15:48:21.837',3,NULL) 

INSERT INTO [dbo].[process] 
            ([client],[processid],[startdate],[enddate],[steps],[groupprocessid] 
) 
VALUES      ('A',4,'2018-05-07 15:48:21.837','2018-05-08 15:48:21.837',9,NULL) 

INSERT INTO [dbo].[process] 
            ([client],[processid],[startdate],[enddate],[steps],[groupprocessid] 
) 
VALUES      ('A',5,'2018-05-09 15:48:21.837','2018-05-10 15:48:21.837',7,NULL) 


Viewing all articles
Browse latest Browse all 76

Trending Articles