Reputation: 101
Im kinda new to SQL Server and am using the 2005 version.
What I need to be able to do is calculate the number of repeat calls that come in.
The table I have called EP_Call_Int has columns called "Daily_Dispo_Date" (datetime) , "Login" (nvarchar(35)), "Policy" (int), "Dispo" (Nvarcar(50)) and has data that looks like this:
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
I have all the test data in a SQL Fiddle Link http://sqlfiddle.com/#!3/0de9c
What im trying to do is count the number of repeat calls our company gets. How I need to count them is by using a time range of 5 minutes to 3 days. So the firstdate will be the first time that person calls in, if they call in again it cant count as a double hit against the same person.
So what im trying to get my final result to look like is:
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
The Query I have so far looks like this:
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
So in short if someone calls in 3 times over the course of 3 days for the same reason it will count as 2 call backs. If they call on day 1, 2, and 3 i need it to show a repeat on day 1 and 2.
Upvotes: 2
Views: 979
Reputation: 40566
I'm not sure I got all the rules right, but I think this might do it:
;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
Update:
After discussing the rules in more depth, I think this is the correct 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
Upvotes: 2