2017-07-25 281 views
0

我试图在MS Access中编写一个查询,并且该查询从两个表中执行两个不同计数的除法。它看起来像这样:作为分母的SQL处理0(零)

Select Count(*)/(Select Count(*) FROM table1 where column1 = userInput) FROM table2; 

当我运行这个,我被要求输入一个值为“userInput”。但有时,列(column1)将不会有该值,它将返回0.是否有办法处理这个问题...例如,我可以在分母为0的任何时刻使分子转向为0

谢谢!

回答

1

您可以将值转换为NULL

Select (Count(*)/
     (Select iif(Count(*) = 0, NULL, COUNT(*)) from table1 where column1 = userInput 
     ) 
     ) 
from table2; 
1

你可以尝试:

Select 
    IIf(
     (Select Count(*) FROM table1 where column1 = userInput) = 0, 
     0, 
     Count(*)/(Select Count(*) FROM table1 where column1 = userInput)) 
From table2;