我的初步问题看似简单,但查看表格会显示我的问题:
**Parts used Model Fleet # QTY in Stock QTY used**
Oil filter TLC 2010 A96 38 1
Oil filter TLC 2010 A82 38 1
Air filter TLC 2010 A96 24 1
Air filter TLC 2010 A82 24 1
Fuel filter TLC 2010 A96 30 1
如何为总油过滤器,空气过滤器和燃油滤清器制作总计,然后从我的库存查询中减去这些总数?
答案 0 :(得分:0)
你的意思是:
SELECT
s.PartsUsed,
s.SumOfQtyUsed,
q.QTYInStock,
[QTYInStock]-[SumOfQtyUsed] AS Result
FROM (
SELECT
PartsUsed,
Sum(QTYUsed) AS SumOfQTYUsed
FROM SampleData
GROUP BY PartsUsed) AS s
INNER JOIN (
SELECT DISTINCT
PartsUsed,
QTYInStock
FROM SampleData) AS q
ON s.PartsUsed = q.PartsUsed