-4
Select (sum(OS.Small) + sum(OS.Medium)) * T.ColorPrice as TotalPrice
from Order_Colors OC
left outer join Order_Selection OS on OS.Order_ColorID = OC.Order_ColorID 
left outer join TShirtPrices T on T.Order_ColorID = OS.Order_ColorID
left outer join OrderInfo O on O.OrderID = OS.OrderID 
where O.CID = 1025
group by OC.Order_ColorID, OC.Color, O.OrderID, T.ColorPrice, T.TShirtPurchaseID

这个查询给了我 1 列“总价格”,大约 20 行。我希望在同一个 sql 查询中获得所有这些行的总和。在 MS SQL 2005 中可以吗?

4

2 回答 2

6

在 SQL Server 中,您可以使用ROLLUP

Select (sum(OS.Small) + sum(OS.Medium)) * T.ColorPrice as TotalPrice
from Order_Colors OC
left outer join Order_Selection OS 
    on OS.Order_ColorID = OC.Order_ColorID 
left outer join TShirtPrices T 
    on T.Order_ColorID = OS.Order_ColorID
left outer join OrderInfo O 
    on O.OrderID = OS.OrderID 
where O.CID = 1025
group by OC.Order_ColorID, OC.Color, O.OrderID, T.ColorPrice, T.TShirtPurchaseID with ROLLUP

这将为您Total Price提供 with for 行,然后您还将获得所有值的最终总计。

于 2013-01-28T16:47:24.983 回答
-3

删除GROUP BY查询的一部分,您将得到一个结果。

于 2013-01-28T16:46:55.373 回答