I have a table full of events. I’ve been asked to create an aggregate table of sessions; one session may have several events. Sessions are identified by having the same arrival time. e.g. (This is a simplification, I’m not typing out actual timestamps):
EventID ArrivalTime StartTime EndTime StaffID 1 0945 0950 0955 John 2 0945 0955 1000 Barb
Might be turned into something like:
ArrivalTime StartTime EndTime StaffID 0945 0950 1000 ???
With use of MIN(StartTime) and MAX(EndTime) to keep it to a single row.
The problem I’m running into, as the question marks above indicate, is getting a single staff ID – which staff member it is doesn’t matter too much, but I need someone. If it were just a string, as I’ve shown above, it could be done with MIN(StaffID), but the thing that’s doing my head in is that I’m required to look up StaffID in the Staff table and pull out the GUID that’s associated with the short code that’s in my table. And GUID’s don’t like functions like MIN(). Also, just to make matters worse, it’s feasible that the StaffID column in the Events table is NULL, so I have to stick with left joins or similar.
Someone suggested a subquery, but apparently my brain refuses to accept this on a Friday and can’t see how to get that to work.
As a baseline, here’s something along the lines of my current query:
SELECT NEWID() AS SessionID,
e.ArrivalTime,
MIN(e.StartTime),
MAX(e.EndTime),
s.StaffGUID
FROM Events e LEFT JOIN Staff s ON e.StaffID = s.StaffID
GROUP BY e.ArrivalTime, s.StaffGUID
The problem is that if two different staff members are in the list, the session will show up twice. Any ideas?
There’s the correlated subselect option (2000+ for using TOP):
…or the derived table/inline view (2005+ for using ROW_NUMBER):
My preference is for the derived table – correlated subqueries tend not to perform as well.