Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 12890

Need help in SLA Calulation and Staging the Records

$
0
0

Hi All,

I am stuck up with calculation of SLA which is a little complicated. I have achived something but that is not suffice.

I have a table with ItemID,Current_Status_major,Current_Date

The value for current_status_major is categorisied into two parts Internal and External.

The calculation should be as follow.

Starttime: Whenever a item status hits the internal State calculation time should start

Endtime: Whenever a item status hits the External State calculation time should stop

In a cycle of item from New till complete, it can hit internal status anytime and go out hit external status and again come back and hit internal status.

Each hit should be considered as one stage and each stage is having 48 hours. If each stage is completed in 48 hours then SLA is met else SLA is breached.

Below pictorial gives a clear idea.

The Date in blue is Start time and in green is end time

In this example, it has 3 stages, simiarly another item can have 10 stages before it completes

So here output

Output should be something like below

Below is the code which i tried. I am near but not upto. Please help.

Set NoCount ON
go
DECLARE @TempInvoice_History table
(
Invoice_Id nvarchar(64)
,Status_Major nvarchar(50)
,UserName nvarchar(50)
,[Date] datetime
)
DECLARE @InvoiceHistory_Table table
(
myId bigint identity(1,1) NOT NULL
,Invoice_ID nvarchar(64)
,Current_Status_Major nvarchar(50)
,Previous_Status_Major nvarchar(50)
,Current_Username nvarchar(50)
,Previous_Username nvarchar(50)
,[Current_Date] datetime
,Previous_Date datetime
)
DECLARE @MyFinalTable table
(
Invoice_Id nvarchar(64)
,SLA_StartTime datetime
,SLA_EndTime datetime
,UserName nvarchar(50)
,SLA_Status nvarchar(20)
)
declare @TempInvoice_Table table(invoice_id nvarchar(64))
Declare 
 @Status_Major nvarchar(50)
,@Username nvarchar(50)
,@Date datetime
,@Invoice_Id nvarchar(64)
,@Temp_Invoice_Id nvarchar(64)
,@Current_Status_Major nvarchar(50)
,@Previous_Status_Major nvarchar(50)
,@Current_Username nvarchar(50)
,@Previous_Username nvarchar(50)
,@Current_Date datetime
,@Previous_Date datetime
,@CurrentFlag int
,@NextFlag int
,@StartDate datetime
,@EndDate datetime
,@TempSM nvarchar(50)
,@TempUN nvarchar(50)
,@TempDT datetime
set @StartDate = '02/01/2013'
set @EndDate = '02/02/2013'
set @Invoice_Id = '0b37fc689faf8b06:2afcacac:13b215716b8:289c'
--insert into @TempInvoice_Table 
--Select DISTINCT Invoice_Id  From teamworks_cott.CG_Invoice_History 
--WHERE ([Date] >= @startdate AND  [Date] < @enddate)
INSERT INTO @TempInvoice_History
SELECT invoice_id,status_major,username,[DATE] from teamworks_cott.CG_Invoice_History
where invoice_id = @invoice_id
--where ([Date] >= @startdate AND  [Date] < @enddate) 
order by [date]
SET @CurrentFlag = 0
SET @NextFlag = 2
DECLARE MyCur cursor for 
select invoice_id,status_Major,Username,[Date] from @TempInvoice_History
--where invoice_id = @invoice_id
order by [date] 

OPEN Mycur
FETCH next from MyCur into @invoice_id,@status_major,@Username,@Date
while @@FETCH_STATUS = 0
BEGIN
	IF @CurrentFlag = @CurrentFlag
		BEGIN
			SET @TempSM = @Current_Status_Major
			SET @TempUN = @Current_Username
			SET @TempDT = @Current_Date
			INSERT into @InvoiceHistory_Table(Invoice_ID,Current_Status_Major,Current_Username,[Current_Date])
			values(@Invoice_Id,@Status_Major,@Username,@Date)
			SET @Current_Status_Major =  @Status_Major
			SET @Current_Username = @Username
			SET @Current_Date = @Date			
			SET @CurrentFlag =	@CurrentFlag + 1
		END
	IF @NextFlag = @CurrentFlag
		BEGIN
			update @InvoiceHistory_Table
			set Previous_Status_Major = @TempSM
			,Previous_Username = @TempUN
			,Previous_Date = @TempDT
			where myId = (Select MAX(myId) from @InvoiceHistory_Table)
			SET @CurrentFlag = @CurrentFlag - 1 
		END
	FETCH next from MyCur into @invoice_id,@status_major,@Username,@Date
END
--
--select * from @InvoiceHistory_Table where Current_Status_Major <> Previous_Status_Major
--order by [Current_Date] asc

--select * from @myStatusTable


CLOSE MyCur
DEALLOCATE MyCur
DECLARE @myStatusTable table
(
myStatus varchar(50), IsInternal bit)
insert into @myStatusTable
values
('Resolved',1),('Coded',1),('Approved',1),('Return from CIR',1),('Return from Parking',1),
('Completed', 0),('Rejected', 0),('Sent for Coding', 0),('Sent for Approval', 0),('Sent for Client Resolution', 0),
('Sent for External Resolution', 0),('Sent for Parking', 0),('Sent for External Email Resolution', 0),
('Sent for Client Internal Resolution', 0),('Sent for Internal Resolution', 0)
;with cte as (
    select    Current_Status_Major,Previous_status_major, 0 Pass,[current_Date],Previous_Date  from @InvoiceHistory_Table-- start with Pass = 0
    union all
    select    C.Current_Status_Major, 
        C.Previous_status_major, 
        CASE
        when 
        C.Current_Status_Major <> P.Previous_Status_Major and m.IsInternal = 1
        then
        P.Pass + 1
        END as 'Stage' ,
        c.[Current_Date],
        c.Previous_Date   -- Increment Pass column with each pass by adding 1
                    -- to the previous value (note that *P*.Pass was used to add to)
    from    @InvoiceHistory_Table as C
        inner join cte P on C.Previous_status_major <> P.Current_Status_Major 
        inner Join @myStatusTable m on p.Previous_Status_Major <> m.myStatus
        where m.IsInternal = 1  
)
select * from cte


Thanks & Regards,
Manjunath C Bhat,
http://manjunathcbhat.blogspot.com
http://manjunathcbhat.wordpress.com


Viewing all articles
Browse latest Browse all 12890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>