开发者

Calculate SLA between same working week SQL Server

开发者 https://www.devze.com 2022-12-13 08:02 出处:网络
I have the following columns in the database.AgreedStartDate, ActualStartDate.I need to apply the following logic: When the AcualStartDate is within the SAME business week as the Agreed开发者_Go百科St

I have the following columns in the database. AgreedStartDate, ActualStartDate. I need to apply the following logic: When the AcualStartDate is within the SAME business week as the Agreed开发者_Go百科StartDate, then it is 'inside' the SLA. If this is not the case then it is 'outside' the SLA. If either of the two columns is NULL, then it is 'incomplete'.


A simple CASE statement? You don't need to use variables, you can just reference your column names instead. I just used variables for a complete statement.

DECLARE @agreedStartDate DATETIME
DECLARE @actualStartDate DATETIME

SET @agreedStartDate = GETDATE()
SET @actualStartDate = GETDATE()

SELECT
    CASE
        WHEN
            @agreedStartDate IS NULL
            OR
            @actualStartDate IS NULL
        THEN
            'Incomplete'
        WHEN
            DATEPART(wk, @agreedStartDate) = DATEPART(wk, @actualStartDate)
            AND
            DATEPART(yyyy, @agreedStartDate) = DATEPART(yyyy, @actualStartDate)
        THEN
            'Inside'
        ELSE
            'Outside'
    END


In SQL Server, you can use a CASE statement like:

select case 
    when ActualStateDate is null or AgreedStartDate is null then 'Incomplete'
    when datepart(wk,ActualStateDate) = datepart(AgreedStartDate) then 'Inside'
    else 'Outside'
    end as ColumnName
from YourTable
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号