2010-12-02 185 views
1

我有一張表,其中包含一個orderidinventoryidquantity - 它的行項目表。該數據庫是SQL Server 2008.SQL Server SUM GROUP BY幫助

我需要知道的是如何編寫一條SQL語句,該語句返回那個訂單的itemid的數量總和,而不包括比它大的訂單orderids。它必須返回orderiditemidtotal

任何幫助?謝謝!

+1

實施例? – 2010-12-02 02:25:03

回答

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