如何计算最大传入呼叫峰值?

时间:2012-10-24 07:24:43

标签: sql-server-2008 tsql datetime date-range

以下是数据库的一部分:

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

在这个数据库中,我想计算最大的callpeaks。换句话说,最多可以同时发生多少呼叫。我在呼叫中心工作,我将生成一份包含这些记录的报告。 SessionID = 24表示来电。任何建议,线索或计算方法对我都有用。

2 个答案:

答案 0 :(得分:1)

当通话开始时,通话次数会增加。呼叫结束时,呼叫次数减少。所以......

;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

答案 1 :(得分:0)

在这种情况下,您需要一个数字表才能加入会话处于活动状态的秒数。必须将此表设置为您可以查询的最长时间长度。

使用这样的表,您可以编写如下查询:

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

此查询给出结果:

11在这些时间打电话:(前1名中的前1名)

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.0​​00
2012-10-16 01:01:48.000
2012-10-16 01:01:19.000
2012-10-16 01:01:20.000