Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

Grouping Consecutive Rows in a Table

$
0
0
SQL Server 2008.

From individual event logs I have generated a table where arrivals and departures at a location are registered per device. As there are multiple registration points, there might be multiple consecutive registrations per location.
If this is the case I need to filter those out and have one registration per location and in the result I need to get the earliest arrival and the latest departure of these consecutive rows.

Part of the table:

logID deviceID Arrived Departed LocationID Grp1 Grp2 Grp
34854 4108 2013-02-07 17:51:05.000 2013-02-07 17:51:15.000 5 1 1 0
34920 4108 2013-02-07 17:51:15.000 2013-02-07 17:51:26.000 5 2 2 0
35002 4108 2013-02-07 17:51:27.000 2013-02-07 17:51:37.000 3 3 1 2
35089 4108 2013-02-07 17:51:41.000 2013-02-07 17:51:54.000 4 4 1 3
35173 4108 2013-02-07 17:51:54.000 2013-02-07 17:52:05.000 4 5 2 3
35265 4108 2013-02-07 17:52:08.000 2013-02-07 17:52:19.000 2 6 1 5
35335 4108 2013-02-07 17:52:19.000 2013-02-07 17:52:36.000 3 7 2 5
35452 4108 2013-02-07 17:52:37.000 2013-02-07 17:52:49.000 2 8 2 6
35521 4108 2013-02-07 17:52:49.000 2013-02-07 17:53:06.000 3 9 3 6
35635 4108 2013-02-07 17:53:07.000 2013-02-07 17:53:20.000 4 10 3 7
35717 4108 2013-02-07 17:53:20.000 2013-02-07 17:53:30.000 4 11 4 7
35800 4108 2013-02-07 17:53:32.000 2013-02-07 17:53:42.000 5 12 3 9
35867 4108 2013-02-07 17:53:43.000 2013-02-07 17:53:53.000 6 13 1 12
35960 4108 2013-02-07 17:53:57.000 2013-02-07 17:54:10.000 3 14 4 10
36058 4108 2013-02-07 17:54:12.000 2013-02-07 17:54:25.000 5 15 4 11
36159 4108 2013-02-07 17:54:28.000 2013-02-07 17:54:38.000 5 16 5 11
36243 4108 2013-02-07 17:54:41.000 2013-02-07 17:54:51.000 5 17 6 11
36329 4108 2013-02-07 17:54:56.000 2013-02-07 17:55:09.000 2 18 3 15
36433 4108 2013-02-07 17:55:11.000 2013-02-07 17:55:24.000 2 19 4 15
36525 4108 2013-02-07 17:55:26.000 2013-02-07 17:55:39.000 4 20 5 15
36622 4108 2013-02-07 17:55:40.000 2013-02-07 17:55:53.000 3 21 5 16
36717 4108 2013-02-07 17:55:56.000 2013-02-07 17:56:09.000 4 22 6 16
36808 4108 2013-02-07 17:56:10.000 2013-02-07 17:56:23.000 4 23 7 16
36905 4108 2013-02-07 17:56:24.000 2013-02-07 17:56:37.000 2 24 5 19
37004 4108 2013-02-07 17:56:39.000 2013-02-07 17:56:52.000 2 25 6 19
37106 4108 2013-02-07 17:56:57.000 2013-02-07 17:57:10.000 5 26 7 19
37199 4108 2013-02-07 17:57:11.000 2013-02-07 17:57:21.000 2 27 7 20
37271 4108 2013-02-07 17:57:21.000 2013-02-07 17:57:31.000 5 28 8 20
37339 4108 2013-02-07 17:57:32.000 2013-02-07 17:57:42.000 2 29 8 21
37412 4108 2013-02-07 17:57:44.000 2013-02-07 17:57:54.000 6 30 2 28


So as long the field LocationID is the same in the next row, it needs to be grouped.

I have added the rows Grp1, Grp2, Grp in an attempt to get an unique grouping number with the following script in the select statement:

,ROW_NUMBER() OVER(PARTITION BY DeviceID
ORDER BY logID) AS Grp1
,ROW_NUMBER() OVER(PARTITION BY DeviceID, LocationID
ORDER BY logID) AS Grp2
,ROW_NUMBER() OVER(PARTITION BY DeviceID
ORDER BY logID)
-
ROW_NUMBER() OVER(PARTITION BY DeviceID, LocationID
ORDER BY logID) AS Grp

By subtracting Grp2 from Grp1 (Grp = Grp1 - Grp2) I hoped to get an unique group number for each set of equal consecutive locations, however the Grp2 column does not restart from 1 each time the LocationID changes: Grp2 in line 7 should have been 1 again, but it is 2 because this is the second row with LocationID = 3 in the list.

I do not know if using ROW_NUMBER is the way to go. I hope some of you have an idea how to tackle this one. It needs to be an effective solution as we easily talk about a table of a few million records....

Thanks in advance.

Viewing all articles
Browse latest Browse all 13329

Trending Articles