【发布时间】:2014-07-06 23:39:33
【问题描述】:
如何在一条语句中获取 Northwind 数据库中 Order Details 表中每个产品的总销售额?
我知道该怎么做:
select productid, UnitPrice * (1 - Discount) * sum(Quantity)
from [Order Details]
group by ProductID,UnitPrice, Discount
我为每个 productid 获取了多行,然后我可以运行另一个查询来为每个 productid 获取一个总和。 请注意,同一产品 ID 的折扣可能不同。
我想在一条 SQL 语句中完成所有操作。怎么样?
【问题讨论】:
-
只使用
group by ProductID -
不起作用。 UnitPrice 和 Discount 必须是聚合函数或在一个组中
-
你可能想要:
sum(UnitPrice * (1 - Discount) * Quantity)
标签: sql sql-server select sum