【发布时间】:2016-09-30 02:16:07
【问题描述】:
我的表中有以下数据:
+-------------+---------------+--------+----------+------------+----------+---------+-------------------------+------------+-----------+--+
| ProductName | PricePerPiece | CafeID | Quantity | TotalPrice | MemberID | OrderID | OrderDate | OrderTotal | ProductID | |
+-------------+---------------+--------+----------+------------+----------+---------+-------------------------+------------+-----------+--+
| Coffee | 10 | 1 | 1 | 10 | 20 | 1008 | 2016-06-01 02:38:47.113 | 196 | 1 | |
| Mocha | 152 | 1 | 1 | 152 | 20 | 1008 | 2016-06-01 02:38:47.113 | 196 | 4 | |
| Pikachu | 34 | 1 | 1 | 34 | 20 | 1008 | 2016-06-01 02:38:47.113 | 196 | 5 | |
| Coffee | 10 | 1 | 1 | 10 | 20 | 1009 | 2016-06-01 02:39:49.750 | 35 | 1 | |
| Cappucino | 25 | 1 | 1 | 25 | 20 | 1009 | 2016-06-01 02:39:49.750 | 35 | 3 | |
+-------------+---------------+--------+----------+------------+----------+---------+-------------------------+------------+-----------+--+
我想要一个特定产品的单行,其中将其 Quantity 和 TotalPrice 添加在一起,得到以下预期输出:
+-------------+---------------+--------+----------+------------+----------+---------+-------------------------+------------+-----------+--+
| ProductName | PricePerPiece | CafeID | Quantity | TotalPrice | MemberID | OrderID | OrderDate | OrderTotal | ProductID | |
+-------------+---------------+--------+----------+------------+----------+---------+-------------------------+------------+-----------+--+
| Coffee | 10 | 1 | 2 | 20 | 20 | 1008 | 2016-06-01 02:38:47.113 | 196 | 1 | |
| Mocha | 152 | 1 | 1 | 152 | 20 | 1008 | 2016-06-01 02:38:47.113 | 196 | 4 | |
| Pikachu | 34 | 1 | 1 | 34 | 20 | 1008 | 2016-06-01 02:38:47.113 | 196 | 5 | |
| Cappucino | 25 | 1 | 1 | 25 | 20 | 1009 | 2016-06-01 02:39:49.750 | 35 | 3 | |
+-------------+---------------+--------+----------+------------+----------+---------+-------------------------+------------+-----------+--+
我尝试了以下查询:
SELECT [ProductName], [PricePerPiece], [CafeID],
SUM([Quantity]) AS [Quantity], SUM([TotalPrice]) AS [TotalPrice],
[MemberID],[OrderID],[OrderDate],[OrderTotal],[ProductID]
FROM [ProductOrders] GROUP BY [ProductID], [ProductName], [PricePerPiece], [CafeID], [MemberID], [OrderID], [OrderDate], [OrderTotal]
但它给出了相同的结果,只是将Coffee 行显示在一起,但没有将其Quantity 和TotalPrice 相加。请告诉我该怎么做?
【问题讨论】:
-
SUM(VALUE1 + VALUE2)在 [sql-server] 中无效吗?
标签: sql sql-server database sql-server-2012 group-by