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

时间:2015-02-18 20:34:29

标签: sql-server if-statement filter case-statement

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

例如:首次报告是从昨天下午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部分收到错误,说明&gt;附近的语法不正确。

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

此后,如果当天下午2点,另一份报告需要回溯到当天上午7点至当天下午2点。需要一些关于如何实现这种逻辑的指导。

案例陈述是否可能,如果是,我将如何编码?我是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点做那个过滤器而不是。

由于

阿都

1 个答案:

答案 0 :(得分:0)

您实际上不需要在此处使用案例陈述。您可以使用AND / OR完成此操作...下面是一个未经测试的示例。请注意,如果此脚本在上午7点,上午11点或下午2点之外运行,则不会返回任何内容。

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))
            )
        )