我使用以下IIF语句继承了Access查询,我试图将其转换为SQL Where条件。我用这个玩了几天不能得到它返回相同的结果。任何帮助将不胜感激。
访问
WHERE (((IIf([EFFDATE]-[MATDATE]<>[PDU_DAYS]-1 And [Calc_Type]="Cycles",IIf([PDU_DAYS] Between [30DayDQMin]+1 And [89DayDQMax]+1,"Yes","No"),IIf([PDU_DAYS] Between 31 And 90,"Yes","No")))="Yes"));
对于SQL代码,我一直在使用各种布尔语句,如下所示。我也试过构建一些案例逻辑。到目前为止没有运气。
([EffDATE]-[MatDate] <> [PDU_Days]-1 and CalcType = 'Cycles' and PDU_Days Between [30DayDQMin]+1 and [89DayDQMax]+1 or PDU_Days Between 31 and 90)
答案 0 :(得分:0)
你应该成功使用几个CASE WHEN x THEN a ELSE b END指令如下:
WHERE
CASE WHEN (([EFFDATE]-[MATDATE]<>[PDU_DAYS]-1) And ([Calc_Type]='Cycles')) THEN
CASE WHEN (([PDU_DAYS] Between [30DayDQMin]+1) And ([89DayDQMax]+1))
THEN 'Yes' ELSE 'No' END
ELSE
CASE WHEN ([PDU_DAYS] Between 31 And 90) THEN 'Yes' ELSE 'No' END
END
= 'Yes'
注意T-SQL单引号样式!
答案 1 :(得分:0)
这是等效的SQL-Server语法:
(
CASE WHEN ([EFFDATE]-[MATDATE] <> [PDU_DAYS]-1 AND [Calc_Type]= 'Cycles') THEN
CASE WHEN ([PDU_DAYS] BETWEEN [30DayDQMin]+1 AND [89DayDQMax]+1) THEN 'Yes' ELSE 'No' END
ELSE
CASE WHEN [PDU_DAYS] BETWEEN 31 AND 90 THEN 'Yes' ELSE 'No' END
END
) = 'yes'
如果[MATDATE]
和[EFFDATE]
是datetime
字段,那么:
(
CASE WHEN (DATEDIFF(dd,[MATDATE],[EFFDATE]) <> [PDU_DAYS]-1 AND [Calc_Type]= 'Cycles') THEN
CASE WHEN ([PDU_DAYS] BETWEEN [30DayDQMin]+1 AND [89DayDQMax]+1) THEN 'Yes' ELSE 'No' END
ELSE
CASE WHEN [PDU_DAYS] BETWEEN 31 AND 90 THEN 'Yes' ELSE 'No' END
END
) = 'Yes'
答案 2 :(得分:0)
你真的不应该需要所有有条件的复杂性,这应该产生相同的结果:
WHERE ([EFFDATE]-[MATDATE]<>[PDU_DAYS]-1
AND [Calc_Type]="Cycles"
AND ([PDU_DAYS] BETWEEN [30DayDQMin]+1 AND [89DayDQMax]+1)
)
OR ([PDU_DAYS] Between 31 And 90)