Calculate Sessions Between TimeRange in TSQL

谁说胖子不能爱 提交于 2019-12-25 05:38:23

问题


Here is part of database:

SessionID    SessionStartTime              SessionCloseTime
24       2012-10-16 01:00:06.000           2012-10-16 01:01:22.000
24       2012-10-16 01:00:08.000           2012-10-16 01:01:10.000
24       2012-10-16 01:00:16.000           2012-10-16 01:01:12.000
24       2012-10-16 01:00:30.000           2012-10-16 01:01:48.000
24       2012-10-16 01:00:41.000           2012-10-16 01:02:08.000
24       2012-10-16 01:00:48.000           2012-10-16 01:01:34.000
24       2012-10-16 01:00:56.000           2012-10-16 01:03:09.000
24       2012-10-16 01:01:02.000           2012-10-16 01:02:13.000
24       2012-10-16 01:01:05.000           2012-10-16 01:03:16.000
24       2012-10-16 01:01:09.000           2012-10-16 01:02:42.000
24       2012-10-16 01:01:15.000           2012-10-16 01:02:48.000
24       2012-10-16 01:01:18.000           2012-10-16 01:02:14.000
24       2012-10-16 01:01:18.000           2012-10-16 01:02:06.000
24       2012-10-16 01:01:42.000           2012-10-16 01:03:16.000
24       2012-10-16 01:01:45.000           2012-10-16 01:03:04.000

I want to add a new column to this table named as SessionCount and then I will be able to see the call peaks I mean max count of sessions which happened at the same time.

This table should be like this:

SessionID    SessionStartTime              SessionCloseTime            SessionCount
24       2012-10-16 01:00:06.000           2012-10-16 01:01:22.000     1
24       2012-10-16 01:00:08.000           2012-10-16 01:01:10.000     2
24       2012-10-16 01:00:16.000           2012-10-16 01:01:12.000     3
24       2012-10-16 01:00:30.000           2012-10-16 01:01:48.000     4
24       2012-10-16 01:00:41.000           2012-10-16 01:02:08.000     5
24       2012-10-16 01:00:48.000           2012-10-16 01:01:34.000     6
24       2012-10-16 01:00:56.000           2012-10-16 01:03:09.000     7
24       2012-10-16 01:01:02.000           2012-10-16 01:02:13.000     8
24       2012-10-16 01:01:05.000           2012-10-16 01:03:16.000     9
24       2012-10-16 01:01:09.000           2012-10-16 01:02:42.000     10
24       2012-10-16 01:01:15.000           2012-10-16 01:02:48.000     10
24       2012-10-16 01:01:18.000           2012-10-16 01:02:14.000     11
24       2012-10-16 01:01:18.000           2012-10-16 01:02:06.000     11
24       2012-10-16 01:01:42.000           2012-10-16 01:03:16.000     7
24       2012-10-16 01:01:45.000           2012-10-16 01:03:04.000     7

so in this table there are maximum 11 call peaks happened at the same time at 01:01:18. There are 11 active calls at 01:01:18. How can I calculate the sessions when session is active then +1 when session is finished then -1?


回答1:


Expanding on the previous answer...

;with ids as 
(
    select *, ROW_NUMBER() over (order by sessionstarttime, sessionclosetime) rid from @t
),
cte as
(
    select SessionStartTime as ChangeTime, 1 as CC, rid from ids    
    union all
    select SessionCloseTime as ChangeTime, -1 as CC, rid from ids    

)
    select ids.id, ids.SessionStartTime,ids.SessionCloseTime, rt
    from
        ids
        inner join
    (
    select cc,rid, changetime,rt from cte
        cross apply 
        (select SUM(cc) as rt from cte c where c.changetime<=cte.changetime) rt             
    ) res
        on ids.rid=res.rid
    where CC=1
    order by SessionStartTime


来源:https://stackoverflow.com/questions/13136296/calculate-sessions-between-timerange-in-tsql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!