0

I need to find a way of grouping rows that occur within 5 minutes of the latest timestamp in the group and the displayid and object id are the same.....

CREATE TABLE [dbo].[ExampleLog](
  [ID] [uniqueidentifier] NOT NULL,
  [EVENTNUMBER] [int] NULL,
  [DISPLAYSERIAL] [int] NULL,
  [DISPLAYID] [varchar](50) NULL,
  [OBJECTSERIAL] [int] NULL,
  [OBJECTRSSILEVEL] [int] NULL,
  [OBJECTID] [varchar](50) NULL,
  [TIMESTAMP] [datetime] NULL)

INSERT INTO [dbo].[ExampleLog]
VALUES 
  (NewID(), 206, 6897913, 'HV 644', 6885819, 66, 'WAP 8', '2012-01-01 12:12:00'),
  (NewID(), 206, 6897913, 'HV 644', 6885819, 66, 'WAP 8', '2012-01-01 12:13:00'),
  (NewID(), 206, 6897913, 'HV 644', 6877807, 54, 'MV 20', '2012-01-01 12:16:00'),
  (NewID(), 103, 6897913, 'HV 644', 6898052, 118, 'HV 62', '2012-01-01 12:20:00'),
  (NewID(), 206, 6897913, 'HV 644', 6886130, 122, 'HV 62', '2012-01-01 12:21:00'),
  (NewID(), 206, 6897913, 'HV 644', 6886130, 106, 'HV 62', '2012-01-01 12:25:00'),
  (NewID(), 204, 6897913, 'HV 644', 6886130, 106, 'HV 62', '2012-01-01 12:31:00'),
  (NewID(), 204, 6897913, 'HV 644', 6886130, 106, 'HV 62', '2012-01-01 12:33:00'),
  (NewID(), 104, 6897913, 'HV 644', 6886130, 106, 'HV 62', '2012-01-01 12:39:00')

so using the dataset above, my results would ideally be grouped as follows;

Rows 1 and 2 are grouped because the objectid and displayid are the same and row 2 occurs within 5 minutes of row 1, there are no other rows in the dataset that contain the same displayid and objectid and a timestamp within 5 minutes of the group’s latest timestamp (row 2) so..

Row 3 is grouped by itself because there are no rows in the dataset where the displayid and objectid match within a 5 minute of that groups latest timestamp (row 3).

Row 4, 5 and 6 are grouped because the displayid's match, the objectid's match and the rows timestamps are within 5 minutes of the last row, row 5's timestamp is within 5 minutes of row 4's, and row 6 timestamp is within 5 minutes of row 5's.

Row 7 and 8 are grouped because the objectid's and displayid's are the same and row 8's timestamp is within 5 minutes of that groups latest timestamp (row 7).

Row 9 is grouped by itself because there are no rows in the dataset where the displayid and objectid match within a 5 minute of that groups latest timestamp

*I am using SQL Server 2008 R2

4

1 Answer 1

0

Thanks for taking a look, and your suggestions, this soloution has worked well for me...

WITH 
    cte_remove AS (
SELECT b.*
FROM dbo.examplelog a
INNER JOIN dbo.examplelog b ON
    a.ID != b.ID AND
    a.DISPLAYID = b.DISPLAYID AND 
    a.OBJECTID = b.OBJECTID AND 
    b.[timestamp] BETWEEN a.[timestamp] AND DATEADD(SECOND, 300, a.[timestamp])
),
   cte_results AS (
SELECT * FROM dbo.examplelog EXCEPT 
SELECT * FROM cte_remove 
)
SELECT * FROM cte_results ORDER BY [timestamp]
GO

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.