gpt4 book ai didi

SQL 查询聚合可能不会出现在 WHERE 子句 (AVG) 中

转载 作者:行者123 更新时间:2023-12-04 09:40:44 26 4
gpt4 key购买 nike

嗨,我找不到如何解决此问题的解决方案。

聚合可能不会出现在 WHERE 子句中,除非它位于包含在 HAVING 子句或选择列表中的子查询中,并且被聚合的列是外部引用。

select p.ProductName, p.UnitPrice
from [DESKTOP-509LB9L\MSSQLSERVER01].northwind.dbo.products p
where p.UnitPrice > AVG(p.UnitPrice)
group by p.UnitPrice

应该是这样的: AVG 是 28.3 所以 UnitPrice 高于 28.3
ProductName                        UnitPrice
Uncle Bob's Organic Dried Pears 30,00
Northwoods Cranberry Sauce 40,00
Mishi Kobe Niku 97,00
Ikura 31,00
Queso Manchego La Pastora 38,00
Alice Mutton 39,00


也没有工作
select p.ProductName, p.UnitPrice
from [DESKTOP-509LB9L\MSSQLSERVER01].northwind.dbo.products p
group by p.UnitPrice
having p.UnitPrice > AVG(p.UnitPrice)

最佳答案

改用子查询:

select p.ProductName, p.UnitPrice
from [DESKTOP-509LB9L\MSSQLSERVER01].northwind.dbo.products p
where p.UnitPrice > (select avg(p2.UnitPrice)
from [DESKTOP-509LB9L\MSSQLSERVER01].northwind.dbo.products p2
);

关于SQL 查询聚合可能不会出现在 WHERE 子句 (AVG) 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62347021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com