我对 SQL Server 有点陌生,并且正在使用 2005 版本。
我需要能够做的是计算进来的重复呼叫的数量。
我称为EP_Call_Int的表有名为 "Daily_Dispo_Date" (datetime) 、 "Login" (nvarchar(35))、 "Policy" (int)、 "Dispo" (Nvarcar(50)) 的列,并且具有如下所示的数据:
Daily_Dispo_Date Login Policy Dispo
2012-03-01 10:31:54 b_smith 42484 Cancellation
2012-03-01 10:45:12 s_tomas 48424 Payment
2012-03-01 11:01:32 b_smith 41546 Billing Question
2012-03-01 11:04:34 s_tomas 42484 Cancellation
2012-03-01 11:15:42 d_jones 48425 Payment
2012-03-01 11:50:02 d_jones 48425 Billing Question
2012-03-01 13:02:09 b_smith 48425 Billing Question
2012-03-02 10:31:54 d_jones 42489 Payment
2012-03-02 10:45:12 s_tomas 48434 Cancellation
2012-03-02 11:01:32 d_jones 41540 Payment
2012-03-02 11:04:34 s_tomas 41546 Billing Question
2012-03-02 11:15:42 d_jones 48417 Payment
2012-03-02 11:50:02 d_jones 44525 Billing Question
2012-03-02 13:02:09 s_tomas 41546 Billing Question
2012-03-03 10:31:54 d_jones 42089 Cancellation
2012-03-03 10:45:12 s_tomas 48434 Cancellation
2012-03-03 11:01:32 d_jones 41440 Cancellation
2012-03-03 11:04:34 s_tomas 41646 Payment
2012-03-03 11:15:42 d_jones 48817 Payment
2012-03-03 11:50:02 d_jones 41546 Payment
2012-03-03 13:02:09 s_tomas 41446 Payment
我在 SQL Fiddle Link http://sqlfiddle.com/#!3/0de9c中有所有测试数据
我想做的是计算我们公司收到的重复电话的数量。我需要如何计算它们是使用 5 分钟到 3 天的时间范围。所以第一次约会将是那个人第一次打电话,如果他们再次打电话,就不能算作对同一个人的双重打击。
所以我试图让我的最终结果看起来像:
Daily_Dispo_Date Dispo Total_Calls Total_Repeating
2012-03-01 Cancellation 2 1
2012-03-01 Payment 2 0
2012-03-01 Billing Question 3 2
2012-03-02 Payment 3 0
2012-03-02 Cancellation 1 1
2012-03-02 Billing Question 3 2
2012-03-03 Cancellation 3 0
2012-03-03 Payment 3 0
到目前为止,我的查询如下所示:
DECLARE @Start DATETIME, @End DATETIME
SET @Start = '20120401'
SET @End = '20120403'
;With [Find_First_Call] As
(
Select
[Policy]
,[Dispo]
,Min([Daily_Dispo_Date]) As [Call_Date] --need to figure out how to have reset after each call
From [EP_Call_Int]
Group By [Policy], [Dispo]
)
Select
DateAdd(dd, DateDiff(dd, 0, [Daily_Dispo_Date]), 0) As [Daily_Dispo_Date]
, [Dispo]
, Count([Dispo]) As [Total_Calls]
,(
Select
Count([EP_Call_Int2].[Dispo])
From [EP_Call_Int] as [EP_Call_Int2]
Left Join [Find_First_Call] as [Find_First_Call] On [Find_First_Call].[Policy] = [EP_Call_Int].[Policy]
And [Find_First_Call].[Dispo] = [EP_Call_Int].[Dispo]
Where [EP_Call_Int2].[Daily_Dispo_Date] >= DateAdd(n, 5, [Find_First_Call].[Call_Date])
And [EP_Call_Int2].[Daily_Dispo_Date] <= DateAdd(dd, 3, [Find_First_Call].[Call_Date])
And DateAdd(dd, 0, [EP_Call_Int].[Daily_Dispo_Date]), 0) = DateAdd(dd, 0, [EP_Call_Int2].[Daily_Dispo_Date]), 0)
) As [Total_Repeat_Calls]
From [EP_Call_Int]
Where [Daily_Dispo_Date] Between @Start And @End
And [Policy] Like '[4]____'
Group By DateAdd(dd, [Daily_Dispo_Date], 0), [Dispo]
Order By [Daily_Dispo_Date], [Total_Calls] Desc
所以简而言之,如果有人在 3 天内出于同样的原因打了 3 次电话,这将算作 2 次回电。如果他们在第 1 天、第 2 天和第 3 天打电话,我需要它在第 1 天和第 2 天显示重复。