How to calculate Maximum incoming Call Peaks?

瘦欲@ 提交于 2019-12-25 05:25:42

问题


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

In this db, I want to calculate max callpeaks. In other words, maximum how much calls are happened at the same time. I am working at a call center and I will generate a report with these records. SessionID = 24 represents incoming calls. Any Suggestions,clues or calculation methods will be useful for me.


回答1:


When a call starts, the number of calls increases. When a call ends, the number of calls decreases. So...

;with cte as
(
    select SessionStartTime as changetime,1 as CC from yourtable
    union all
    select SessionCloseTime,-1 from yourtable
)
    select top 1 changetime,rt from
    (
    select * from cte
        cross apply 
        (select SUM(cc) as rt from cte c where c.changetime<=cte.changetime) rt         
    ) v
    order by rt desc



回答2:


Sounds like you need a number table in this case to join for the amount of seconds the sessions are alive. This table must be set to the maximum time length in seconds you will be able to query.

With such a table you can write a query like this:

CREATE TABLE Numbers 
( 
    Number INT IDENTITY(1,1) PRIMARY KEY CLUSTERED 
) 

WHILE COALESCE(SCOPE_IDENTITY(), 0) <= 1000000
BEGIN 
    INSERT #Numbers DEFAULT VALUES 
END

CREATE TABLE #Calls
(
    SessionID int,
    SessionStartTime datetime,
    SessionCloseTime datetime
)


INSERT INTO #Calls VALUES (24, '2012-10-16 01:00:06.000', '2012-10-16 01:01:22.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:00:08.000', '2012-10-16 01:01:10.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:00:16.000', '2012-10-16 01:01:12.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:00:30.000', '2012-10-16 01:01:48.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:00:41.000', '2012-10-16 01:02:08.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:00:48.000', '2012-10-16 01:01:34.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:00:56.000', '2012-10-16 01:03:09.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:02.000', '2012-10-16 01:02:13.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:05.000', '2012-10-16 01:03:16.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:09.000', '2012-10-16 01:02:42.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:15.000', '2012-10-16 01:02:48.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:18.000', '2012-10-16 01:02:14.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:18.000', '2012-10-16 01:02:06.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:42.000', '2012-10-16 01:03:16.000')
INSERT INTO #Calls VALUES (24, '2012-10-16 01:01:45.000', '2012-10-16 01:03:04.000')


SELECT TOP 1 DATEADD(SECOND, Number, SessionStartTime) CallTime, COUNT(C.SessionId) NoOfCalls FROM #Calls C
INNER JOIN #Numbers N ON N.Number <= DATEDIFF(SECOND, C.SessionStartTime, C.SessionCloseTime) --Join number of seconds between start and end time
GROUP BY DATEADD(SECOND, Number, SessionStartTime)
ORDER BY NoOfCalls DESC

This query gives the result:

11 Calls at these times: (With top 1 one of these ofcourse)

2012-10-16 01:01:47.000
2012-10-16 01:01:22.000
2012-10-16 01:01:21.000
2012-10-16 01:01:46.000
2012-10-16 01:01:48.000
2012-10-16 01:01:19.000
2012-10-16 01:01:20.000



来源:https://stackoverflow.com/questions/13044565/how-to-calculate-maximum-incoming-call-peaks

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