IF THAN 或 CASE 语句取决于 where 子句中的当前时间

IF THAN or CASE Statement depending on current time in where clause

我正在尝试创建一个 运行 在不同时间运行的脚本,并且每次都必须是不同的过滤器。

例如:第一份报告是从昨天下午 2 点到今天早上 7 点。

类似于:

AND CASE
    WHEN  DATEADD(day, datediff(day, 0, getdate()), 0) + '7:00'
THEN   clm.createDtTm >= DATEADD(HOUR, 14,CAST(DATEADD(DAY,-1,CAST(GETDATE() AS DATE)) AS DATETIME))
AND  clm.createDtTm <= DATEADD(HOUR, 7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
ELSE 
END

但是我在 >= 的 THEN 部分遇到错误,说 > 附近的语法不正确。

这可能是因为在 WHEN 和 THEN 中都有一个表达式,因为我认为你只能有一个或另一个。

在此之后,如果是当天下午 2 点,另一个报告需要 运行 回到当天早上 7 点到当天下午 2 点。需要一些关于如何实现该逻辑的指导。

是否可以使用 case 语句?如果可以,我该如何编码?我是 SQL 的新手,如果您需要更多信息,请联系我。

查询

SELECT Nai.clmNum AS Claim_Num, coverage, FirstName, LastName, payee,  checkNum, checkAmount, tranType, 
Act.State, Act.AccNum, clm.createDtTm
FROM    dbo.Financial(NOLOCK) AS clm, dbo.CLMs(NOLOCK) AS   Nai,dbo.Account_NIGHTLY(nolock) AS Act  
WHERE   coverage in ('Injury', 'Property Damage', 'Liability')
AND     checkNum IS NOT NULL
AND     clm.fldrID = Nai.fldrID
AND     tranType = 'payment'
AND     Act.State = 'MA'
AND     Nai.AccNum = Act.AccNum

CASE --IF its 7AM send a report out filtered on createDtTm between yesterday 2pm(14) to current day 7am(7)

--AND       clm.createDtTm >= DATEADD(HOUR, 14,CAST(DATEADD(DAY,-1,  CAST(GETDATE() AS DATE)) AS DATETIME))
--AND       clm.createDtTm <= DATEADD(HOUR, 7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))

CASE --If its 11AM send a report out filtered on createDtTm between current day 7am(7) to current day 11am(11)

--AND       clm.createDtTm >= DATEADD(HOUR, 7,CAST(DATEADD(DAY,0, CAST(GETDATE() AS DATE)) AS DATETIME))
--AND       clm.createDtTm <= DATEADD(HOUR, 11, CAST(CAST(GETDATE() AS DATE) AS DATETIME))

CASE -- If its 2PM send a report out filtered on createDtTm between current day 11am(11) to current day 2pm(14)

--AND       clm.createDtTm >= DATEADD(HOUR, 11,CAST(DATEADD(DAY,0, CAST(GETDATE() AS DATE)) AS DATETIME))
--AND       clm.createDtTm <= DATEADD(HOUR, 14, CAST(CAST(GETDATE() AS DATE) AS DATETIME))

添加了我想要实现的查询和逻辑,此查询将在这些时间范围内 运行 通过计划到 运行 的存储过程。我希望它检查正确的时间,如果是早上 7 点,则检查在第一个时间范围内是否发生了新的财务状况并发送报告。如果是上午 11 点,则执行该过滤器。

谢谢

阿卜杜勒

实际上你不需要在这里使用case语句。您可以使用 AND/OR 完成此操作...下面是一个未经测试的示例。请注意,如果此脚本在上午 7 点、上午 11 点或下午 2 点以外的时间 运行,它将 return 什么都没有。

SELECT Nai.clmNum AS Claim_Num, coverage, FirstName, LastName, payee,  checkNum, checkAmount, tranType, 
Act.State, Act.AccNum, clm.createDtTm
FROM    dbo.Financial(NOLOCK) AS clm, dbo.CLMs(NOLOCK) AS   Nai,dbo.Account_NIGHTLY(nolock) AS Act  
WHERE   coverage in ('Injury', 'Property Damage', 'Liability')
AND     checkNum IS NOT NULL
AND     clm.fldrID = Nai.fldrID
AND     tranType = 'payment'
AND     Act.State = 'MA'
AND     Nai.AccNum = Act.AccNum

AND     (
            --IF its 7AM send a report out filtered on createDtTm between yesterday 2pm(14) to current day 7am(7)
            (
                DATEPART(hh, GETDATE()) = 7
                AND clm.createDtTm >= DATEADD(HOUR, 14,CAST(DATEADD(DAY,-1,  CAST(GETDATE() AS DATE)) AS DATETIME))
                AND clm.createDtTm <= DATEADD(HOUR, 7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
            )
            OR
            --If its 11AM send a report out filtered on createDtTm between current day 7am(7) to current day 11am(11)
            (
                DATEPART(hh, GETDATE()) = 11
                AND clm.createDtTm >= DATEADD(HOUR, 7,CAST(DATEADD(DAY,0, CAST(GETDATE() AS DATE)) AS DATETIME))
                AND clm.createDtTm <= DATEADD(HOUR, 11, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
            )
            OR
            -- If its 2PM send a report out filtered on createDtTm between current day 11am(11) to current day 2pm(14)
            (
                DATEPART(hh, GETDATE()) = 14
                AND clm.createDtTm >= DATEADD(HOUR, 11,CAST(DATEADD(DAY,0, CAST(GETDATE() AS DATE)) AS DATETIME))
                AND clm.createDtTm <= DATEADD(HOUR, 14, CAST(CAST(GETDATE() AS DATE) AS DATETIME))
            )
        )