如何在一张表中按差异记录总计所有产品?

时间:2013-11-07 17:22:02

标签: sql vb.net visual-studio-2010

我有下表,我想得到所有产品的总数:

prodcod | prodname | pricein | Qty | priceout

01      | Coca     | 12$     | 40  | 20$
02      | pepsi    | 6$      | 20  | 10$
03      | fanta    | 9$      | 50  | 15$
04      | M150     | 12$     | 10  | 20$

我想得到总数Qty = 120

2 个答案:

答案 0 :(得分:1)

SELECT SUM(Qty) AS TOTAL

FROM [TABLE_NAME]

答案 1 :(得分:1)

SQL代码很简单:

SELECT SUM(Qty) FROM Products

从vb.net调用:

Dim Quantity As Integer
Using cn As New SqlConnection("connection string here"), _
      cmd As New SqlCommand("SELECT SUM(Qty) FROM Products", cn)

    cn.Open()
    Quantity = DirectCast(cmd.ExecuteScalar(), Integer)
End Using