Asp classic 如何使用vbscript对列的值求和

Asp classic 如何使用vbscript对列的值求和,asp-classic,vbscript,ado,Asp Classic,Vbscript,Ado,我试图获取一列的值之和,为此我在select语句中使用sum() <% sql = "select SUM(OpcCalEstQuantity) as qt_total from [Sheet1$] where cInt(JobEstimateNumber) = '"&cint(request.QueryString("no"))&"' and MccDescription = 'CTP Plate Making & Plates' and

我试图获取一列的值之和,为此我在select语句中使用sum()

<%
            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
            %>

所以我认为解决方法应该是使用vbscript来计算值。但是没有这样的函数来计算列中的值。

我不太喜欢SQL和MS Jet引擎,但我认为要求和的列包含一些空值。若要摆脱它们,并且您的数据库支持它们,您可以使用以下功能:

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

如果您想在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>

希望这有帮助,

Erik

JobEstimateNumber
很可能包含一个或多个空值。我手头没有现成的解决方案,但我首先要问的是,为什么需要将列转换为整数,而不是首先将其存储为整数。
<%
    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>