所以我必须显示产品的名称,id产品和所有价格超过所有产品平均价格的产品的单价。这是我的问题
select ProductID, ProductName, UnitPrice
from Products
where UnitPrice in
(select UnitPrice
from Products
where avg(UnitPrice)> UnitPrice)但是我得到的是由于aggregat avg(unitprice)的错误,当我用它的原因错误替换它的时候。我能做什么?
发布于 2020-10-20 23:46:52
,所以我必须显示产品的名称,id产品和所有产品的单价,这些产品的价格超过了所有产品的平均价格。
关。使用子查询返回平均值,并将其用于比较:
select ProductID, ProductName, UnitPrice
from Products p
where UnitPrice > (select avg(UnitPrice) from Products);发布于 2020-10-20 23:51:23
您可以为此使用窗口函数,而不是子查询:
select *
from (
select ProductID, ProductName, UnitPrice, avg(unitPrice) over() avgUnitPrice
from Products
) p
where UnitPrice > avgUnitPrice发布于 2020-10-20 23:52:21
我想你要找的东西是这样的
with avg_cte(avg_price) as (
select avg(UnitPrice)
from Products)
select ProductID, ProductName, UnitPrice
from Products p
cross join avg_cte a
where p.UnitPrice>a.avg_price;https://stackoverflow.com/questions/64448713
复制相似问题