0

我正在尝试获取一列的值的总和,因为我在我的选择语句中使用了 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
            %>

但是在浏览器上显示它时出现此错误。

Microsoft JET Database Engine error '80040e14'

Invalid use of Null 

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

4

2 回答 2

1

如果您想在 SQL 中解决这个问题,coalesce 是一个很好的建议。
如果你想纯粹在 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>

希望这会有所帮助,
埃里克

于 2012-06-04T12:29:48.753 回答
1

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

sql = "select SUM(COALESCE(OpcCalEstQuantity, 0)) as qt_total from ......"
于 2012-06-04T08:34:13.863 回答