这个问题与电话信息有关,而我正在使用Genesys http://www.genesyslab.com,但我更喜欢在这里发帖提问,因为这是一个与SQL相关的问题。我有两张桌子:
(表1:“ CallerID ”)一个包含CallerID和ConnID的表(标识一个调用)。
(表2:“ Callinfo ”)另一个表包含CallerID,呼入日期(以int
格式存储,带有翻译它的方式)和状态行,它对应于我将在下面解释的动作。一个示例行是
ConnID StartTime Status
--------------------- ----------- -----------
30119914212527698 1300286888 2
每次呼叫进入两个表都会更新。
我需要做的是从Callinfo
获取状态为2或4的最后两天(这些是未回答的呼叫)的所有呼叫,并从CallerID
表中获取CallerID。
之后,我需要检查相同的CallerID是否在其ConnID的Callinfo表检查中有新的寄存器。如果这些新寄存器的状态不同于2 o 4(意味着通信成功),我应该删除第一个查询的CallerID。
查询的目的是调用那些在过去两天内尝试调用但未能调用的客户端。
第一个查询正常,你应该忽略DATEDIFF函数。
DECLARE @twoDays BIGINT
SET @twoDays = 172800 --48hs expressed in seconds
SELECT DISTINCT ConnID, StartTime, CID.CallerID, Status FROM Callinfo
INNER JOIN dbo.CallerID CID
ON Callinfo.ConnID = CID.ConnID
WHERE
(Status = 2 OR Status = 4)
AND
StartTime > DATEDIFF(second, '1970-01-01 00:01:00', GETUTCDATE())- @twoDays
我不知道如何使用结果并检查是否存在状态不同为2和4的新呼叫。
我正在研究SQL Server 2005。
以下序列可以清除: 如果调用者A的情况如此:
1/1 12pm: A call OK
1/1 1pm: A call FAIL
然后我应该得到结果。但如果情况是这样的话:
1/1 12pm: A call OK
1/1 1pm: A call FAIL
2/1 12pm: A call OK
然后我不应该得到A作为结果。因此,当我检查3/1时,我不会得到结果,因为A的最后一次呼叫是正常的,并且在4/1呼叫者A不应该出现在第一次查询中。
答案 0 :(得分:2)
这样的事情应该更快。
DECLARE @twoDaysAgo DateTime
SET @twoDaysAgo = DATEADD(day,-2,GETDATE())
WITH MissedCalls AS
(
SELECT DISTINCT ConnID, StartTime, CID.CallerID, Status FROM Callinfo
JOIN dbo.CallerID CID ON Callinfo.ConnID = CID.ConnID
WHERE Status IN (2,4)
AND StartTime > @twoDaysAgo
), NotMissedCalls AS
(
SELECT DISTINCT ConnID, StartTime, CID.CallerID, Status
FROM Callinfo
JOIN dbo.CallerID CID ON Callinfo.ConnID = CID.ConnID
WHERE Status not in (2,4)
AND StartTime > @twoDaysAgo
-- following line may speed up or slow down depending on table sizes and indexes
AND CID.CallerID in (SELECT CALLERID FROM MissedCalls)
)
SELECT *
FROM MissedCalls
WHERE NOT CallerID IN (SELECT CallerID FROM NotMissedCalls)
注意:我没有测试,所以我可能有错字。为了安全起见,我为所有查询添加了所有列,但我希望这可以改进。 (较少的列应该稍快一点。)
这解决了新问题。
DECLARE @twoDaysAgo DateTime
SET @twoDaysAgo = DATEADD(day,-2,GETDATE())
WITH MissedCalls AS
(
SELECT ConnID, Max(StartTime) as LastFailDate, CID.CallerID FROM Callinfo
JOIN dbo.CallerID CID ON Callinfo.ConnID = CID.ConnID
WHERE Status IN (2,4)
AND StartTime > @twoDaysAgo
GROUP BY ConnID, CID.CallerID
), NotMissedCalls AS
(
SELECT ConnID,Max(StartTime) as LastGoodDate,CID.CallerID
FROM Callinfo
JOIN dbo.CallerID CID ON Callinfo.ConnID = CID.ConnID
WHERE Status not in (2,4)
AND StartTime > @twoDaysAgo
-- following line may speed up or slow down depending on table sizes and indexes
AND CID.CallerID in (SELECT CALLERID FROM MissedCalls)
GROUP BY ConnID, CID.CallerID
), CallList AS
(
SELECT LastFailDate, LastGoodDate, Bad.ConnID, Bad.CallerID
FROM MissedCalls Bad
JOIN NotMissedCalls Good ON Bad.ConnID = Good.ConnID AND Bad.CallerID = Good.CallerID
)
SELECT LastFailDate, ConnID, CallerID
FROM CallList
WHERE (LastGoodDate > LastFailDate) OR (LastGoodDate IS NULL)
答案 1 :(得分:1)
我想我有一个工作基地。
DECLARE @twoDays BIGINT
SET @twoDays = 172800 --48hs expressed in seconds
SELECT DISTINCT ConnID, StartTime, CID.CallerID, Status FROM Callinfo
INNER JOIN dbo.CallerID CID
ON Callinfo.ConnID = CID.ConnID
WHERE
(Status = 2 OR Status = 4)
AND
StartTime > DATEDIFF(second, '1970-01-01 00:01:00', GETUTCDATE())- @twoDays
AND CID.CallerID NOT IN (
SELECT DISTINCT CID.CallerID FROM Callinfo
INNER JOIN dbo.CallerID CID
ON Callinfo.ConnID = CID.ConnID
WHERE
(Status <> 2 AND Status <> 4)
AND
StartTime > DATEDIFF(second, '1970-01-01 00:01:00', GETUTCDATE())- @twoDays
)
此查询将在第二个查询中显示时删除callerID:也就是说在过去2天内设置了呼叫的呼叫者。
缺陷:无法检测A是否呼叫第1天,召回第2天并且失败(范围是第1天到第3天)。但是当你在第2天到第4天尝试时,他会在下次检查时加注。