Subscribe to Windows IT Pro
November 10, 2009 12:00 AM

Calculating Concurrent Sessions, Part 2

A better set-based solution
SQL Server Pro
InstantDoc ID #102926
Rating: (0)

Listing 2: Solution Based on Window Aggregate Functions

WITH Events AS

(

  SELECT app, starttime AS ts, 1 AS event_type FROM dbo.Sessions

  UNION ALL

  SELECT app, endtime, -1 FROM dbo.Sessions

),

Counts AS

(

  SELECT app,

    SUM(event_type) OVER(PARTITION BY app

                         ORDER BY ts, event_type

                         ROWS BETWEEN UNBOUNDED PRECEDING

                                  AND CURRENT ROW) AS cnt

  FROM Events

)

SELECT app, MAX(cnt) AS mx

FROM Counts

GROUP BY app;

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.