Good day,
How can I do like sumifs in sql query in vb.net
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
here is my code. The plan is to take the Sum of column prdInput based on the helper which is txtlot txtPartnumber and txprocess.
I managed to get display the first prdinput the query found but I need the the sum of its since I have multiple txtlot partnumber and process.
Thanks
Edit: I already did the SUM(prdInput) on my query but nothing shows on the textbox.
Advertisement
Answer
You’re almost there. You just needed to alias the column that you performed the sum aggregate on:
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
It might be easier to see the difference with a formatted query:
SELECT SUM(prdInput) AS prdInput --you forgot to give this column a name FROM ProdOutput WHERE HELPER = '" & txtLot.Text & "-" & txtPartNumber.Text & "-" & txtProcess1.Text & "';