SQL Server SUM GROUP BY帮助

时间:2010-12-02 02:17:46

标签: tsql sql-server-2008 group-by

我的表格包含orderidinventoryidquantity - 它是一个订单项表。该数据库是SQL Server 2008。

我需要知道的是如何编写一个SQL语句,该语句返回该订单中itemid的数量总和,而不是计算比orderids更大的订单。它必须返回orderiditemidtotal

有任何帮助吗?谢谢!

1 个答案:

答案 0 :(得分:1)

猜测:

SELECT
    SUM(quantity) AS total, --"sums of quantities for that itemid at that order"
    orderid, inventoryid --"It must return the orderid, itemid"
WHERE
    orderid < (some larger order id value) --"not counting orders that have larger orderids"
GROUP BY
    orderid, inventoryid