Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在一条语句中获取Northwind数据库中Order Details表中每个产品的总销售额?_Sql_Sql Server_Select_Sum - Fatal编程技术网

Sql 如何在一条语句中获取Northwind数据库中Order Details表中每个产品的总销售额?

Sql 如何在一条语句中获取Northwind数据库中Order Details表中每个产品的总销售额?,sql,sql-server,select,sum,Sql,Sql Server,Select,Sum,如何在一条语句中获取Northwind数据库中Order Details表中每个产品的总销售额 我知道怎么做: select productid, UnitPrice * (1 - Discount) * sum(Quantity) from [Order Details] group by ProductID,UnitPrice, Discount 我为每个productid获取多行,然后可以运行另一个查询,为每个productid获取一个总和请注意,对于相同的product

如何在一条语句中获取Northwind数据库中Order Details表中每个产品的总销售额

我知道怎么做:

select productid,  UnitPrice * (1 - Discount) * sum(Quantity) 
  from [Order Details]
    group by ProductID,UnitPrice, Discount
我为每个productid获取多行,然后可以运行另一个查询,为每个productid获取一个总和请注意,对于相同的productid,折扣可以不同


我想在一条SQL语句中完成这一切。如何计算?

您可以提取
sum
函数,对每一行的价格进行单独求和,而不只是对数量求和(这在数学上应该是可以的,因为乘法大于加法):


仅使用
groupbyproductid
不起作用。单价和折扣必须是一个聚合函数或一个组,您可能需要:
sum(单价*(1-折扣)*数量)
SELECT   productid,  SUM(UnitPrice * (1 - Discount) * Quantity)
FROM     [Order Details]
GROUP BY ProductID