用于组合电话数据的SQL查询超时

时间:2015-10-29 19:26:52

标签: sql sql-server multiple-columns reportingservices-2005 unpivot

我有一个SELECT语句(见下文),我正在运行,并且只想显示我在另一个表中的数字的数据。查询运行时间太长,我正在寻找有关如何优化的可能反馈:

SELECT Connect.PortName
       , Count(Calls.DialedNumber) AS Dials
       , Count(DISTINCT Calls.DialedNumber) AS Numbers
       , CONVERT(CHAR(8), Dateadd(second, Sum(Datediff(second, Calls.StartTime, Calls.EndTime)), 0), 108) AS CustMinSec
       , CONVERT(VARCHAR, Dateadd(ms, Sum(Connect.TalkTimeSeconds * 1000), 0), 108) AS TalkTime
       , CONVERT(CHAR(8), Dateadd(second, Sum(Datediff(second, 0, Connect.RingTime)), 0), 108) AS RingTime
       , CONVERT(CHAR(8), Dateadd(second, Sum(Datediff(second, 0, Connect.HoldTime)), 0), 108) AS HoldTime
FROM   Calls
       INNER JOIN Connect ON Calls.ID = Connect.CallTableID
WHERE  ( CONVERT(CHAR(10), Calls.StartTime, 101) = '10/26/2015' )
   AND ( Calls.CallType = 3 )
   AND ( Connect.GroupID = 14 )
   AND ( Calls.DialedNumber IN (SELECT DISTINCT '+1'
                                                + Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Replace(Upper(PHONE), 'A', ''), 'B', ''), 'C', ''), 'D', ''), 'E', ''), 'F', ''), 'G', ''), 'H', ''), 'I', ''), 'J', ''), 'K', ''), 'L', ''), 'M', ''), 'N', ''), 'O', ''), 'P', ''), 'Q', ''), 'R', ''), 'S', ''), 'T', ''), 'U', ''), 'V', ''), 'W', ''), 'X', ''), 'Y', ''), 'Z', ''), '-', ''), '(', ''), ')', '') AS MAIN
                                FROM   CUSTTABLE
                                WHERE  ( DATAAREAID = 'SHA' )) )
GROUP  BY Connect.PortName
ORDER  BY Connect.PortName 

这仅显示主号码,但我还需要搜索传真号码,手机号码和附加号码。只关注1列的事实是一个问题。

任何指导都会受到赞赏,我可能会让这更难。我有另一个版本为所有列执行UNPIVOT,但它超时了所以我简化了这个。我在带有SSRS报表服务器前端的SQL 2005服务器上运行。

0 个答案:

没有答案