How do I get the count for time periods?
This is what worked for me in the end.

SELECT 
     [StartTime].[PossibleTime] AS [StartTime]
    ,[EndTime].[PossibleTime] AS [EndTime]
    ,(SELECT COUNT(1) 
       FROM [Log] AS [L] 
       WHERE [StartTime].[PossibleTime] &gt;= [L].[loginTime]
       AND [EndTime].[PossibleTime] &lt;= [L].[logoutTime]
     ) AS [UserCount]
SELECT ROW_NUMBER() OVER (ORDER BY [PossibleTime]) AS [ROWNUM]
FROM (
    SELECT [loginTime] AS [PossibleTime]
    FROM [Log]
    UNION ALL
    SELECT [logoutTime] AS [PossibleTime]
    FROM [Log]
   ) AS [EndTime]
INNER JOIN
SELECT ROW_NUMBER() OVER (ORDER BY [PossibleTime]) AS [ROWNUM]
FROM (
    SELECT [loginTime] AS [PossibleTime]
    FROM [Log]
    UNION ALL
    SELECT [logoutTime] AS [PossibleTime]
    FROM [Log]
) AS [StartTime]
ON [StartTime].[ROWNUM] = [EndTime].[ROWNUM]
ORDER BY [StartTime].[PossibleTime], [EndTime].[PossibleTime]