I have the table of following structure:
UserID StartedOn EndedOn
1 2009-7-12T14:01 2009-7-12T15:01
2 2009-7-12T14:30 2009-7-12T1
A naive approach:
You can test if another user b is currently logged in when user a logs in with
a.StartedOn BETWEEN b.StartedOn AND b.EndedOn
And someone has to be the "final logon" to the set of "the most concurrent users".
If you now go through all records (as a) and check how many other users (b) where logged in at the time and then order the list (desc) the first result is the maximum number of concurrent users.
SELECT
a.id, a.UserId, a.StartedOn, a.EndedOn,
(
SELECT
Count(*)
FROM
logons as b
WHERE
a.StartedOn BETWEEN b.StartedOn AND b.EndedOn
) as c
FROM
logons as a
ORDER BY
c desc
And now read Database development mistakes made by application developers to see how inefficient (or even wrong) this is ;-)
e.g. you have a large temporary table that the order by operates on without any index to help the sql server.
(and btw: I tested this with MySQL because I don't have a sql server at hand right now)