首页 > 解决方案 > vb.net 的 SUMIFS

问题描述

再会,

如何在 vb.net 的 sql 查询中做 sumifs

    Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
    Try
        query = "SELECT SUM(prdInput) FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
        retrieveSingleResult(query)

        proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()


    Catch ex As Exception
    End Try


End Sub

这是我的代码。计划是根据帮助器 txtlot txtPartnumber 和 txprocess 获取列 prdInput 的总和。

我设法显示找到的查询的第一个 prdinput 但我需要它的总和,因为我有多个 txtlot 零件号和进程。

谢谢

编辑:我已经在查询中执行了 SUM(prdInput),但文本框中没有显示任何内容。

标签: sqlvb.netsum

解决方案


您快到了。您只需为执行 sum 聚合的列起别名:

    Private Sub txtProcYM_TextChanged(sender As Object, e As EventArgs) Handles txtProcYM.TextChanged
    Try
        query = "SELECT SUM(prdInput) AS prdInput FROM ProdOutput WHERE HELPER ='" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';"
        retrieveSingleResult(query)

        proc1QTYIn.Text = dt.Rows(0)("prdInput").ToString()


    Catch ex As Exception
    End Try


End Sub

使用格式化查询可能更容易看出差异:

SELECT
  SUM(prdInput) AS prdInput --you forgot to give this column a name
FROM ProdOutput
WHERE HELPER = '" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';

推荐阅读