SELECT
ISNULL(Customer.Name, ' GRAND') AS CustomerName,
ISNULL(Item.ItemName, 'TOTAL') AS ItemName,
SUM(Item_Order.PriceAtDate) AS Price
FROM
Item_Order
INNER JOIN
Item ON Item_Order.ItemID = Item.ItemID
INNER JOIN
TheOrder ON Item_Order.OrderID = TheOrder.OrderID
INNER JOIN
Customer ON TheOrder.CustomerID = Customer.CustomerID
GROUP BY
Customer.Name, Item.ItemName, Item_Order.PriceAtDate WITH ROLLUP
ORDER BY
Customer.Name
結果:ROLLUP只總結了不同的值在SQL Server 2008中
CustomerName ItemName Price
------------ ------------------- -------
GRAND TOTAL 1380.46
Adrian Hammer 21.88
Adrian Hammer 21.88
Adrian Soldering Iron 30.54
Adrian Soldering Iron 30.54
Adrian TOTAL 52.42
Baker Valve 21.88
Baker Valve 21.88
Baker TOTAL 21.88
正如你可以看到使用匯總給出的總計增加了30.54和21.88這涉及到52.42,但兩個項目是有序的兩倍。所以我需要實際的總數,而不僅僅是爲了不同的值。
有誰知道我做錯了什麼?
歡迎使用StackOverflow:如果您發佈代碼,XML或數據樣本,請**在文本編輯器中突出顯示這些行,然後單擊編輯器工具欄上的「代碼示例」按鈕(「{}」),以良好地格式化和語法突出顯示它! –