2014-04-29 168 views
1

我有一個查詢涉及到一個Extranet報告,我最近被要求調整以排除週末的SLA計算 - 我無能爲力 - 有人能請幫忙嗎?從SLA中排除週末日期

SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 

/* 
Modified Date: Modified By: Description: 
19/03/2014  RC    Created for extranet report 'IA Booking to IA timings' 
29/04/2014  MC    Adjusted SLAs 
*/ 

ALTER PROCEDURE [dbo].[rex_IABookingToIA] 
@Start_Date DATETIME, 
@End_Date DATETIME 

AS 


set dateformat dmy 
--declare @Start_Date DATETIME 
--declare @End_Date DATETIME 
--set @Start_Date = '10/03/2014' 
--set @End_Date = '11/03/2014' 


SELECT 
    AppointmentBookedBy 
    ,count(CaseID) AS [Total IA Bookings] 
    ,SUM(CASE WHEN datediff(dd,AppointmentBooked_DateTime,[Date]) BETWEEN 0 and 2 
      THEN 1 ELSE 0 
     END) AS [0 - 2 days] 
    ,SUM(CASE WHEN datediff(dd,AppointmentBooked_DateTime,[Date]) BETWEEN 3 and 5 
      THEN 1 ELSE 0 
     END) AS [3 - 5 days]  
    ,SUM(CASE WHEN datediff(dd,AppointmentBooked_DateTime,[Date]) BETWEEN 6 and 7 
      THEN 1 ELSE 0 
     END) AS [6 - 7 days] 
    ,SUM(CASE WHEN datediff(dd,AppointmentBooked_DateTime,[Date]) > 7 
      THEN 1 ELSE 0 
     END) AS [8 days and over] 
INTO #IABookings       
FROM TICCSMI.dbo.array_PW_appointments[dbo] 
WHERE AppointmentBooked_DateTime >= @Start_Date  
and AppointmentBooked_DateTime < @End_Date 
and [Type]='IA' 
and [Department Booked By] = 'Physioworld Booking Team' 
GROUP BY AppointmentBookedBy 
ORDER BY AppointmentBookedBy 


SELECT 
    AppointmentBookedBy AS [Agent] 
    ,[Total IA Bookings] 
    ,[0 - 4 days] 
    ,CASE WHEN ([0 - 2 days] = 0 OR [Total IA Bookings] = 0) THEN CAST(0 AS DECIMAL (10,2)) 
     ELSE CAST(100.00*[0 - 2 days]/[Total IA Bookings] AS DECIMAL (10,2)) 
    END AS [%] 
    ,[3 - 5 days] 
    ,CASE WHEN ([3 - 5 days] = 0 OR [Total IA Bookings] = 0) THEN CAST(0 AS DECIMAL (10,2)) 
     ELSE CAST(100.00*[3 - 5 days]/[Total IA Bookings] AS DECIMAL (10,2)) 
    END AS [%] 
    ,[6 - 7 days] 
    ,CASE WHEN ([6 - 7 days] = 0 OR [Total IA Bookings] = 0) THEN CAST(0 AS DECIMAL (10,2)) 
     ELSE CAST(100.00*[6 - 7 days]/[Total IA Bookings] AS DECIMAL (10,2)) 
    END AS [%] 
    ,[8 days and over] 
    ,CASE WHEN ([8 days and over] = 0 OR [Total IA Bookings] = 0) THEN CAST(0 AS DECIMAL  (10,2)) 
     ELSE CAST(100.00*[8 days and over]/[Total IA Bookings] AS DECIMAL (10,2)) 
    END AS [%] 
FROM #IABookings 
ORDER BY AppointmentBookedBy 

所以 - 我需要ABL埃託奧拿起如果預訂是在週末提出,但被的SLA部分時間不包括週末。

在此先感謝

回答

0

我已經使用遞歸CTE來完成此過去。

這個想法是找到@Start_Date之前的星期六作爲種子來建立所有其他週末日期的列表,直到@End_Date

在報告查詢中,您將LEFT JOIN設置爲CToint週末日期在AppointmentBooked_DateTime和Date之間。

因爲我們使用了LEFT JOIN,所以沒有跨過週末的任何約會都會有該列的NULL,而跨越一個週末的約會每週末將有一個記錄。 COUNT不計算NULL,因此計算除週末之外的SLA只需從DATEDIFF中減去COUNT(weekendDate)即可。

下面是一些SQL與一對夫婦的測試用例,我相信是沿着你正在尋找線:

declare @Start_Date DATE = '20140404' 
declare @End_Date DATE = '20140503' 
SET DATEFIRST 7 -- just in case 
declare @SaturdayBeforeStartDate date 
select @SaturdayBeforeStartDate = dateadd(day, -datepart(weekday, @Start_date), @Start_date) 

declare @array_PW_appointments table(CaseID int, AppointmentBookedBy varchar(20), AppointmentBooked_DateTime datetime, [Date] datetime, [Type] char(2), [Department Booked By] varchar(50)) 
insert @array_PW_appointments values 
-- test case 1: 8 days, spanning one weekend. This should be 6 days ex. weekend 
(1, 'Jane','20140408','20140416','IA','Physioworld Booking Team'), 
-- test case 2: 3 days, no weekend. Should be 3 days. 
(2, 'Jane','20140421','20140424','IA','Physioworld Booking Team'), 
-- test case 3: 14 days, two full weekends. Should be 10 days. 
(3, 'Jane','20140409','20140423','IA','Physioworld Booking Team'), 
-- test case 4: 2 days, starting on a Sunday. Should be 1 day 
(4, 'Jane','20140427','20140428','IA','Physioworld Booking Team') 

;with weekend_cte as (
    select @SaturdayBeforeStartDate as weekendDate 
    union all 
    -- if it's Saturday, add 1 day; if Sunday, add 6 to get to the following Saturday 
    select dateadd(day, case DATEPART(weekday, weekendDate) when 7 then 1 when 1 then 6 end, weekendDate) 
    from weekend_cte 
    where weekendDate <= @End_Date 
), aged as (
    select CaseID, AppointmentBookedBy, AppointmentBooked_DateTime, [Date] 
    -- excluding weekends is as simple as subtracting the # of rows the left join actually hits on 
    , datediff(dd,AppointmentBooked_DateTime,[Date])-count(weekendDate) as age_exweekend 
    FROM @array_PW_appointments a 
    -- left join so we don't miss Cases without a weekend in between 
    left join weekend_cte w on w.weekendDate >= AppointmentBooked_DateTime and w.weekendDate <= [Date] 
    WHERE AppointmentBooked_DateTime >= @Start_Date  
    and AppointmentBooked_DateTime < @End_Date 
    and [Type]='IA' 
    and [Department Booked By] = 'Physioworld Booking Team' 
    group by CaseID, AppointmentBookedBy, AppointmentBooked_DateTime, [Date] 
), bucketed_exweekend as (
    SELECT AppointmentBookedBy 
    ,count(CaseID) AS [Total IA Bookings] 
    ,SUM(CASE WHEN age_exweekend BETWEEN 0 and 2 THEN 1 ELSE 0 END) AS [0 - 2 days] 
    ,SUM(CASE WHEN age_exweekend BETWEEN 3 and 5 THEN 1 ELSE 0 END) AS [3 - 5 days]  
    ,SUM(CASE WHEN age_exweekend BETWEEN 6 and 7 THEN 1 ELSE 0 END) AS [6 - 7 days] 
    ,SUM(CASE WHEN age_exweekend > 7 THEN 1 ELSE 0 END) AS [8 days and over] 
    FROM aged 
    GROUP BY AppointmentBookedBy 
) 
select AppointmentBookedBy, [Total IA Bookings] 
, [0 - 2 days], CASE WHEN ([Total IA Bookings] = 0) THEN 0.0 ELSE CAST(100.00*[0 - 2 days]/[Total IA Bookings] AS DECIMAL (10,2)) END AS [%] 
, [3 - 5 days], CASE WHEN ([Total IA Bookings] = 0) THEN 0.0 ELSE CAST(100.00*[3 - 5 days]/[Total IA Bookings] AS DECIMAL (10,2)) END AS [%] 
, [6 - 7 days], CASE WHEN ([Total IA Bookings] = 0) THEN 0.0 ELSE CAST(100.00*[6 - 7 days]/[Total IA Bookings] AS DECIMAL (10,2)) END AS [%] 
, [8 days and over], CASE WHEN ([Total IA Bookings] = 0) THEN 0.0 ELSE CAST(100.00*[8 days and over]/[Total IA Bookings] AS DECIMAL (10,2)) END AS [%] 
from bucketed_exweekend