• The only thing I'm seeing is the way that you populate your @splitTimes table variable. I'd change it to a set-based method:

    -- See Jeff Moden's article

    -- The "Numbers" or "Tally" Table: What it is and how it replaces a loop.

    -- at http://www.sqlservercentral.com/articles/T-SQL/62867/.

    -- NOTE! A permanent tally table will always be MUCH faster

    -- than this inline one. See the above article to create your own!

    INSERT INTO @splitTimes

    SELECT [Time] = DATEADD(minute, N-@interval, '00:00')

    FROM Tally

    WHERE N <= 1200 + @interval -- # of minutes to span 00:00 to 20:00

    AND N % @interval = 0; -- allows any interval desired

    Edit: Actually, I do see another thing. Your where clause. By using a function on the dates being searched, you are eliminating the use of any indexes that might be available. Rewrite them so that they are in the format:

    WHERE sess.sessionstart >= <your starting date>

    AND sess.sessionend < <your ending date + 1 interval)

    In the case of what you're using:

    WHERE sess.sessionstart >= @day -- 20101122 00:00

    AND sess.sessionend < DateAdd(hour, 20, DateAdd(minute, 1, @day)) -- 20101122 20:01

    Wayne
    Microsoft Certified Master: SQL Server 2008
    Author - SQL Server T-SQL Recipes


    If you can't explain to another person how the code that you're copying from the internet works, then DON'T USE IT on a production system! After all, you will be the one supporting it!
    Links:
    For better assistance in answering your questions
    Performance Problems
    Common date/time routines
    Understanding and Using APPLY Part 1 & Part 2