Hi all,
I have a table of operator events. OperatorID and EventTime are the important columns. What I want is to return all of the events but to identify groups of consecutive events by operator without cursors or looping.
example (simplified):
OperatorID EventTime
1 10:00
1 11:00
1 12:00
2 14:00
2 15:00
1 16:00
1 17:00
3 18:00
should return
OperatorID EventTime GroupNumber
1 10:00 1
1 11:00 1
1 12:00 1
2 14:00 2
2 15:00 2
1 16:00 3
1 17:00 3
3 18:00 4
I've been messing around with
which correctly partitions the set but only gives row numbers within each parition. I need something that identifies the partition itself.
--Adam
--"He who knows best knows how little he knows." - Thomas Jefferson
I have a table of operator events. OperatorID and EventTime are the important columns. What I want is to return all of the events but to identify groups of consecutive events by operator without cursors or looping.
example (simplified):
OperatorID EventTime
1 10:00
1 11:00
1 12:00
2 14:00
2 15:00
1 16:00
1 17:00
3 18:00
should return
OperatorID EventTime GroupNumber
1 10:00 1
1 11:00 1
1 12:00 1
2 14:00 2
2 15:00 2
1 16:00 3
1 17:00 3
3 18:00 4
I've been messing around with
Code:
SELECT OperatorID, EventTime,
ROW_NUMBER() OVER(PARTITION BY OperatorID ORDER BY EventTime)
from OperatorEvents
which correctly partitions the set but only gives row numbers within each parition. I need something that identifies the partition itself.
--Adam
--"He who knows best knows how little he knows." - Thomas Jefferson