如果Else Ladder在MS Access SQL查询中

时间:2015-06-18 07:47:05

标签: sql if-statement ms-access-2010

如何在MS Access SQL查询中编写If else Ladder,使用Recordset编写不在代码中

我有这样的条件:

SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' 
AND SpecialistCode = 'C4535421' AND WeekNum = 1

它会给出任何值,如果没有,我想运行这个查询:

SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' 
AND SpecialistCode = 'C4535421' AND (WeekNum is Null or WeekNum = '')

它会给出任何值,如果没有,我想运行这个查询:

SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' 
AND (SpecialistCode is null or SpecialistCode ='') AND WeekNum = 1

它会给出任何值,如果没有,我想运行这个查询:

SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' 
AND (SpecialistCode is null or SpecialistCode ='') AND (WeekNum is Null or WeekNum = '') 

它会给出任何值,如果没有,我想运行这个查询:

SELECT min(ThresholdValue) FROM tbl_Threshold WHERE (SpecialityCode is null 
or SpecialityCode ='') AND (SpecialistCode is null or SpecialistCode ='') AND WeekNum = 1

它会给出任何值,如果没有,我想运行这个查询:

SELECT min(ThresholdValue) FROM tbl_Threshold WHERE (SpecialityCode is null 
or SpecialityCode ='') AND (SpecialistCode is null or SpecialistCode ='')      
AND (WeekNum is Null or WeekNum = '') 

基本上,我在查询中给出3个条件,如果没有找到该条件的记录,那么我将逐步替换空值或null的条件。目前我正在使用记录集,意味着检查记录集的计数,如果为0,则用下一个条件填充记录集,依此类推。但是这种方法需要花费很多时间,因为这本身就是围绕100多个记录循环完成的。所以我能做到的任何其他方式都可以在一个查询中吗?

1 个答案:

答案 0 :(得分:0)

考虑嵌套的IIF语句。注意反转的条件层次结构:

SELECT min(IIF((SpecialityCode is null or SpecialityCode ='') AND (SpecialistCode is null or SpecialistCode ='') AND (WeekNum is Null or WeekNum = ''), 
ThreshodValue,
IIF((SpecialityCode is null or SpecialityCode ='') AND (SpecialistCode is null or SpecialistCode ='') AND (WeekNum = 1),
ThresholdValue,
IIF(SpecialityCode = '130' AND (SpecialistCode is null or SpecialistCode ='') AND (WeekNum is Null or WeekNum = ''),
ThresholdValue,
IIF(SpecialityCode = '130' AND (SpecialistCode is null or SpecialistCode ='') AND WeekNum = 1, 
ThresholdValue,
IIF(SpecialityCode = '130' AND SpecialistCode = 'C4535421' AND (WeekNum is Null or WeekNum = ''), 
ThresholdValue,
IIF(SpecialityCode = '130' AND SpecialistCode = 'C4535421' AND WeekNum = 1, 
ThresholdValue, 0)))))))
FROM tbl_threshold;

或者,您可以考虑一个Union查询,其中如果将排除没有值的select语句,并且union(vs union all)会阻止重复。但是可能你想在union查询中返回id和thresholdvalues,并在另一个查询中使用min()返回聚合联合查询。

SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' AND SpecialistCode = 'C4535421' AND WeekNum = 1
UNION
SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' AND SpecialistCode = 'C4535421' AND (WeekNum is Null or WeekNum = '')
UNION
SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' AND (SpecialistCode is null or SpecialistCode ='') AND WeekNum = 1
UNION
SELECT min(ThresholdValue) FROM tbl_Threshold WHERE SpecialityCode = '130' AND (SpecialistCode is null or SpecialistCode ='') AND (WeekNum is Null or WeekNum = '')
UNION
SELECT min(ThresholdValue) FROM tbl_Threshold WHERE (SpecialityCode is null or SpecialityCode ='') AND (SpecialistCode is null or SpecialistCode ='') AND WeekNum = 1
UNION
SELECT min(ThresholdValue) FROM tbl_Threshold WHERE (SpecialityCode is null or SpecialityCode ='') AND (SpecialistCode is null or SpecialistCode ='') AND (WeekNum is Null or WeekNum = '');