-6

How can I calculate the difference between the purchase and sale quantity in one query using Ms Access database?

My data, for example, looks like this:

ProductId Type Quantity
1         Purchase 24
1         Sale      1

How would I get the difference of (24-1=23) in one query?

4

2 回答 2

0

您可以自行加入表:

SELECT p.productId, (p.quanity - COALESCE(s.quantity, 0)) difference
FROM table p
LEFT JOIN table s
ON p.type = 'Purchase' AND s.type = 'Sale' AND p.productId = s.productId
于 2013-09-16T11:03:04.810 回答
0

我想你有数据库名称 [DB-NAME]。列和行类似于。

[Table1]
ProductID      Quantity         Purchase         Sale
-----------    ---------        ---------        --------
1                1                 24              1
2                100               50              10

如果要计算特定产品 ID 的 [Purchase] - [Sale] 使用:

( Select (Purchase - Sale) AS MyNumber FROM[DB-Name].[Table1] WHERE (ProductID=1))

//其中1是您的产品ID

结果表将是

MyNumber
--------
23

如果要计算所有 [ProductID] 的总数,请使用:

  (Select (SUM(Purchase) - Sum(Sale)) AS MyNumber FROM[DB-Name].[Table1] )

结果表将是

MyNumber
--------
63
于 2013-09-16T11:12:25.000 回答