[英]Subquery to sum values from a column
I use this code to sum an order for a certain date: 我使用以下代码对某个日期的订单求和:
select od.OrderID, (od.unitprice * quantity) - ((od.unitprice * od.quantity) * discount) + freight as OrderSum
from orders, [Order Details] as od
where orderdate = '1996-12-12' and od.orderid = orders.orderid
The code above gives the following output table: 上面的代码提供以下输出表:
OrderID | OrderSum
10380 370,37
10380 506,63
10381 119,99
Desired end result and output: I want to sum the values in the created column "OrderSum" above so that the output would be put in a column named "Total ordersum". 所需的最终结果和输出:我想对上面创建的“ OrderSum”列中的值求和,以便将输出放置在名为“ Total ordersum”的列中。 In other words, the desired output table I actually want: 换句话说,我实际上想要的输出表是:
Total ordersum
996,99
How can I add a subquery to the code above to solve this? 如何在上面的代码中添加子查询来解决此问题? I would be very thankful if someone could give a code sample. 如果有人可以提供代码示例,我将非常感谢。
Just do an aggregation query: 只需执行聚合查询:
select sum(od.unitprice * quantity) - sum((od.unitprice * od.quantity) * discount) + sum(freight) as Total_OrderSum
from orders o join
[Order Details] as od
on od.orderid = o.orderid
where orderdate = '1996-12-12';
Note: Never use commas in the FROM
clause. 注意: 请勿在FROM
子句中使用逗号。 Always use explicit JOIN
syntax with the conditions in the ON
clause. 始终对ON
子句中的条件使用显式JOIN
语法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.