我一直以为我不能,但是MSDN另有说法。
When GROUP BY is not used, HAVING behaves like a WHERE clause.
我检查并得到错误:
Msg 8121:
Column '...' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY.
那么,它是什么?文档中有错误,还是鲜为人知的细节?
这可能是 Sybase SQL Server 中有效的功能之一。现在,微软重写了 SQL Server,使其更接近 ANSI 标准,但忘记修复文档。
是这样吗?
最佳答案
您可以在不使用group by
但在聚合函数上使用having
。
select avg(price) from tbltemp having avg(price) >= 2
在 MSDN您提供的链接,给出的示例指示我们使用 where 而不是having。
You may NOT write something like "select avg(price) from goods HAVING price >= 1000" but you may write "select avg(price) from goods WHERE price >= > 1000"
关于sql-server - 我可以在 SQL 查询中使用 HAVING 而不是 WHERE 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28761521/