2017-07-25 280 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;