How to select a single record per segment based on priority

by bradja   Last Updated July 12, 2019 05:26 AM

I have a list of Activities actioned by during each segment (StartTime, EndTime). Activities have a defined Priority (TASK = 7, CHAT = 10) with 10 being the highest.

+--------+------------------+------------------+----------+----------+
| UserID |     StartTime    |      EndTime     | ACTIVITY | PRIORITY |
+--------+------------------+------------------+----------+----------+
|  ABCD  | 2019/07/01 09:00 | 2019/07/01 09:15 |   TASK   |     7    |
+--------+------------------+------------------+----------+----------+
|  ABCD  | 2019/07/01 09:15 | 2019/07/01 09:45 |   TASK   |     7    |
+--------+------------------+------------------+----------+----------+
|  ABCD  | 2019/07/01 09:15 | 2019/07/01 09:45 |   CHAT   |    10    |
+--------+------------------+------------------+----------+----------+
|  ABCD  | 2019/07/01 09:45 | 2019/07/01 10:00 |   TASK   |     7    |
+--------+------------------+------------------+----------+----------+

I need to associate a single activity for each segment per user. That is, for the user ABCD in segment 09:15-09:45 there is currently 2 activities (TASK and CHAT). Based on their priorities, the CHAT should be assigned to this segment.

There will be multiple users with activities that can start/end at various times throughout the day and the solution will need to allow for this.

Expected Output

+--------+------------------+------------------+----------+----------+
| UserID |     StartTime    |      EndTime     | ACTIVITY | PRIORITY |
+--------+------------------+------------------+----------+----------+
|  ABCD  | 2019/07/01 09:00 | 2019/07/01 09:15 |   TASK   |     7    |
+--------+------------------+------------------+----------+----------+
|  ABCD  | 2019/07/01 09:15 | 2019/07/01 09:45 |   CHAT   |    10    |
+--------+------------------+------------------+----------+----------+
|  ABCD  | 2019/07/01 09:45 | 2019/07/01 10:00 |   TASK   |     7    |
+--------+------------------+------------------+----------+----------+

Scenario Data

DECLARE @ActivityLog TABLE (UserID VARCHAR(20), Activity VARCHAR(8), Priority INT, StartTime DATETIME, EndTime DATETIME)

INSERT INTO @ActivityLog VALUES ('ABCD', 'TASK', 7, '2019/07/01 09:00', '2019/07/01 10:00')
INSERT INTO @ActivityLog VALUES ('ABCD', 'CHAT', 10, '2019/07/01 09:15', '2019/07/01 09:45')

I'm stuck at this point trying to work out how to proceed further with solving this problem. Any solutions or advice will be much appreciated. Cheers.



Answers 1


Using ROW_NUMBER along with TOP 1 WITH TIES we can try:

SELECT TOP 1 WITH TIES UserID, StartTime, EndTime, ACTIVITY, PRIORITY
FROM @ActivityLog
ORDER BY ROW_NUMBER() OVER (PARTITION BY UserID, StartTime, EndTime ORDER BY PRIORITY DESC);
Tim Biegeleisen
Tim Biegeleisen
July 12, 2019 05:25 AM

Related Questions


Updated December 06, 2017 20:26 PM

Updated March 08, 2017 15:26 PM

Updated July 30, 2018 06:26 AM

Updated February 16, 2018 20:26 PM

Updated October 30, 2017 17:26 PM