一列中所有字段的总和

时间:2013-04-16 10:22:59

标签: sql sum

我试图计算一列中所有数字的总和。

列名是“单位”类型的整数。

这应该有用吗?

cmd3 = New OleDbCommand("SELECT SUM(Units) FROM tblJobs WHERE BookedOut = NULL AND HoldDate = NULL ", con)

lblLiveUnits.Text = cmd3.ExecuteNonQuery()

谢谢

1 个答案:

答案 0 :(得分:3)

您的查询不正确,您不想制作=NULL您应该使用IS NULL的内容:

SELECT SUM(Units)  As TotalUnits
FROM tblJobs 
WHERE BookedOut IS NULL 
  AND HoldDate IS NULL

然后在您的代码中,您将使用ExecuteScalar

Int32 lblLiveUnits = 0;

cmd3 = New OleDbCommand("SELECT SUM(Units)  As TotalUnits FROM tblJobs WHERE BookedOut IS NULL AND HoldDate IS NULL ", con);

lblLiveUnits = cmd3.ExecuteScalar()