这是我的sql当我尝试执行它然后我收到错误消息msg 在预期条件的上下文中指定的非布尔类型的表达式,接近'then'。 < / p>
我无法理解我犯错误的地方。所以请特别查看我的完整SQL,我会使用嵌套的情况并告诉我应该纠正什么。
SELECT
[bbajobs].[jid],
[Add Dates],
CASE WHEN 1 then 'Yes'
CASE WHEN 0 then
CASE WHEN job_flow_state.no_fault_found = 1 THEN 'No fault found'
CASE WHEN job_flow_state.unable_to_repaired = 1 THEN 'Unable to repair'
CASE WHEN job_flow_state.Repair_Not_Requested = 1 THEN 'Repair Not Requested'
ELSE 'N/A'
END
END AS [Repaired]
FROM bbajobs
LEFT JOIN ourfeedback
ON bbajobs.jid = ourfeedback.jid
INNER JOIN job_flow_state
ON bbajobs.jid = job_flow_state.jid
WHERE CONVERT(VARCHAR(8), bbajobs.jobshippeddate, 112) >='20140117'
AND CONVERT(VARCHAR(8), bbajobs.jobshippeddate, 112) <='20140117' AND bbajobs.jobstate IN ('DONE')
AND bbajobs.jobtype NOT LIKE '%warranty%'
AND job_flow_state.repaired = 1
AND (ltrim(rtrim(ourfeedback.Rating))='' OR ltrim(rtrim(ourfeedback.Rating))='N/A') AND [bbajobs].[accountreference] IN
(SELECT accountref FROM ourfeedback where
CONVERT(VARCHAR(8), ourfeedback.adddates, 112) >='20140117' AND CONVERT(VARCHAR(8), ourfeedback.adddates,112) <= '20140117'
)
CASE WHEN 1 then 'Yes'
CASE WHEN 0 then
CASE WHEN job_flow_state.no_fault_found = 1 THEN 'No fault found'
CASE WHEN job_flow_state.unable_to_repaired = 1 THEN 'Unable to repair'
CASE WHEN job_flow_state.Repair_Not_Requested = 1 THEN 'Repair Not Requested'
ELSE 'N/A'
END
END AS [Repaired]
答案 0 :(得分:1)
我会像这样重写嵌套的case语句:
CASE WHEN 1 then 'Yes'
ELSE
CASE WHEN job_flow_state.no_fault_found = 1 THEN 'No fault found'
WHEN job_flow_state.unable_to_repaired = 1 THEN 'Unable to repair'
WHEN job_flow_state.Repair_Not_Requested = 1 THEN 'Repair Not Requested'
ELSE 'N/A'
END
END AS [Repaired]
答案 1 :(得分:0)
你的CASE
太多了 - 你只需要在内部嵌套中WHEN
s。您还需要打开谓词 - 即'什么'是1
或0
。最后,你应该考虑如果内部情况不匹配会发生什么。
即:
CASE SomeColumn
WHEN 1 then 'Yes'
WHEN 0 then
CASE WHEN job_flow_state.no_fault_found = 1 THEN 'No fault found'
WHEN job_flow_state.unable_to_repaired = 1 THEN 'Unable to repair'
WHEN job_flow_state.Repair_Not_Requested = 1 THEN 'Repair Not Requested'
ELSE 'Oops'
END
ELSE 'N/A'
END AS [Repaired];