罗斯文(Northwind)总和定单值并用平均值检查它们

时间:2019-06-11 19:28:52

标签: sql sql-server northwind

我使用Northwind数据库。我得到的公司名称及其订单值的总和(以下代码)。现在,我想检查公司的订单价值是否大于平均水平,但我从查询中获得的平均价值与总和相同。如果Orders_Value> avg_order_value,我也无法查询。

我尝试这样做:

having Orders_Value > avg_order_value;

但它不适用于:

SELECT CompanyName, AVG(Orders_Value) as avg_order_value
FROM
(SELECT  C.CompanyName, SUM(OD.Quantity * OD.UnitPrice) as Orders_Value 
FROM dbo.Customers as C
JOIN dbo.Orders as O on C.CustomerID = O.CustomerID
JOIN dbo.[Order Details] as OD on O.OrderID = OD.OrderID
GROUP BY  C.CompanyName)
as inner_query
GROUP BY  CompanyName;

0 个答案:

没有答案