总和和连接不正确

时间:2013-09-08 02:48:37

标签: sql-server tsql

我有两张桌子,一张是产品清单,另一张是收银台。

我写过这个问题:

SELECT tblComponents.Name, tblComponents.Tax, 
SUM(CONVERT(int,ProductCheckout.ItemQuantity)) as Quantity
FROM tblComponents
INNER JOIN ProductCheckout
ON tblComponents.ID=ProductCheckout.ItemID where tblComponents.Tax <> 0
Group BY tblComponents.Name, ProductCheckout.ItemQuantity, tblComponents.Tax;

我希望得到订购商品的总数/数量。

目前显示

OrderName   2.00    1
OrderName   2.00    2

我希望它显示:

OrderName   4       3

我希望获得所有产品名称的清单,以及所订税额和数量的总和。

我不确定这个查询有什么问题,但我需要指出正确的方向。

1 个答案:

答案 0 :(得分:2)

我认为您不应该在ProductCheckout.ItemQuantity子句中加入tblComponents.TaxGroup By,而应该对tblComponents.Tax列进行总结。类似的东西:

SELECT tblComponents.Name, SUM(tblComponents.Tax), 
       SUM(CONVERT(int,ProductCheckout.ItemQuantity)) as Quantity
FROM tblComponents
INNER JOIN ProductCheckout 
    ON tblComponents.ID=ProductCheckout.ItemID where tblComponents.Tax <> 0
Group BY tblComponents.Name;