2012-06-04 38 views
0

我想獲得列的值的總和,爲此,我在我的select語句中使用SUM()。如何使用vbscript總結列的值

<% 
      sql = "select SUM(OpcCalEstQuantity) as qt_total from [Sheet1$] where cInt(JobEstimateNumber) = '"&cint(request.QueryString("no"))&"' and MccDescription = 'CTP Plate Making & Plates' and MaoOperationDescription = 'Plate Making'" 
      rs1.open sql, con, 1, 2 
      do while not rs1.eof 
      %> 
      <td style="padding:3px; text-align:right;"><%=rs1("qt_total")%></td> 
      <% 
       rs1.movenext 
       loop 
       rs1.close 
      %> 

但我在瀏覽器上顯示時出現此錯誤。

Microsoft JET Database Engine error '80040e14' 

Invalid use of Null 

所以我認爲解決方法是用vbscript來計算值。但是沒有這樣的函數來計算列的值。

+0

列'JobEstimateNumber '最有可能包含一個或多個NULL值。我手頭沒有解決方案,但我首先會問,爲什麼需要將列轉換爲整數,而不是將其作爲整數存儲在第一位。 –

回答

1

我不是很注意SQL和MS Jet引擎,但我認爲你想SUM的列包含一些NULL值。爲了擺脫他們,如果你的數據庫支持它,你可以使用coalesce功能,如:

sql = "select SUM(COALESCE(OpcCalEstQuantity, 0)) as qt_total from ......" 
1

聚結是,如果你想在SQL來解決這個問題一個很好的建議。
如果你想在VBScript/ASP純粹解決它,你將不得不環路和自己計算的總金額,試試這個:

<% 
    sql = "select OpcCalEstQuantity from [Sheet1$] where cInt(JobEstimateNumber) = '"&cint(request.QueryString("no"))&"' and MccDescription = 'CTP Plate Making & Plates' and MaoOperationDescription = 'Plate Making'" 
    rs1.open sql, con, 1, 2 
%> 

<% dim total : total = 0 
    do while not rs1.eof 
     if NOT(isNull(rs1("OpcCalEstQuantity")) OR rs1("OpcCalEstQuantity")="") then total = total + cDbl(rs1("OpcCalEstQuantity")) 
     rs1.movenext 
    loop 
    rs1.close 
%> 
<td style="padding:3px; text-align:right;"><%=total%></td> 

希望這有助於
埃裏克