0

我无法弄清楚这段代码有什么问题。在 Where 子句中添加 Case 语句之前,查询工作正常,但是当我添加它时,我得到了那个错误。如果有人可以帮助我,我将不胜感激!!

这是案例陈述

CASE 
    WHEN (GETDATE()< '2013-06-30 00:00:00.000' AND GETDATE() >= '2013-04-01 00:00:00.000') THEN 
        CASE WHEN (TargetStartDt >= '2013-04-01 00:00:00.000' AND TargetStartDt < '2013-06-30 00:00:00.000') THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)')
        END 
    WHEN (GETDATE() < '2013-09-30 00:00:00.000' AND GETDATE() >= '2013-07-01 00:00:00.000') THEN 
        CASE WHEN TargetStartDt < '2013-06-30 00:00:00.000' THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')
             WHEN (TargetStartDt >= '2013-07-01 00:00:00.000' AND TargetStartDt < '2013-09-30 00:00:00.000') THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)')
        END  
    WHEN GETDATE() < '2013-12-31 00:00:00.000' AND GETDATE()>= '2013-10-01 00:00:00.000' THEN
        CASE WHEN TargetStartDt < '2013-09-30 00:00:00.000' THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')
             WHEN (TargetStartDt >= '2013-10-01 00:00:00.000' AND TargetStartDt < '2013-12-31 00:00:00.000')  THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)')
        END 
    --ELSE (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')
END

太感谢了!!

4

2 回答 2

2

CASE是一个返回单个值的表达式。它不能用于布尔或流控制逻辑。因此你不能说:

THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)'
   OR JobStatus = 'Closed(Not Filled)')

您需要以不同的方式构建此子句。我不会尝试对您的整个查询进行逆向工程,但也许CASE它应该是一个单一的而不是嵌套的:

CASE WHEN (date range checks) AND 
  (JobStatus IN ('Approved', 'Closed(Filled)', 'Closed(Not Filled)')) 
THEN ...
于 2013-07-29T15:01:49.470 回答
0

这看起来像你的问题:

THEN (JobStatus = 'Approved' OR JobStatus = 'Closed(Filled)' OR JobStatus = 'Closed(Not Filled)')

这种类型的子句出现在几个地方。我不确定您要做什么,但不应该遵循布尔表达式THEN

于 2013-07-29T15:01:47.723 回答