2

我对 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 天显示重复。

4

1 回答 1

2

我不确定我是否所有规则都正确,但我认为这可能会做到:

;with Call_ordinal_no as (
  select 
    convert(date, [Daily_Dispo_Date]) as CallDate,
    [Login],
    [Dispo],
    row_number() over (
      partition by 
        convert(date, [Daily_Dispo_Date]),
        [Login],
        [Dispo]
      order by [Daily_Dispo_Date]
    ) as CallNumber
  from [EP_Call_Int]
)
select 
  CallDate as Daily_Dispo_Date,
  Dispo,
  count(*) as Total_Calls,
  sum(case when CallNumber > 1 then 1 else 0 end) as Total_Repeating
from Call_ordinal_no
group by CallDate, Dispo
order by CallDate;

SQLFiddle:http ://sqlfiddle.com/#!3/0de9c/34

更新:

在更深入地讨论了规则之后,我认为这是正确的 SQL:

;with calls as (
  select 
    DateAdd(dd, DateDiff(dd, 0, c.[Daily_Dispo_Date]), 0) as CallDate,
    c.[Login],
    c.[Dispo],
    c.[Policy],
    case 
      when first.[Login] is null then 0
      else 1 
    end as IsRepeat
  from [EP_Call_Int] c
  left join [EP_Call_Int] first
    on c.[Login] = first.[Login]
    and c.[Dispo] = first.[Dispo]
    and c.[Policy] = first.[Policy]
    and datediff(minute, first.[Daily_Dispo_Date], c.[Daily_Dispo_Date])
      between 5 and 3 * 24 * 60
)
select 
  CallDate,
  Dispo,
  count(*) as Total_Calls,
  sum(IsRepeat) as Total_Repeating
from calls
group by CallDate, Dispo
order by CallDate, Dispo

SQLFiddle:http ://sqlfiddle.com/#!3/0de9c/69

于 2012-05-08T21:01:10.143 回答