我一直在通过一些sql语法来研究oracle sql考试,我发现了一些令人困惑的东西
基于official引用,select语法如下:
SELECT [ hint ] [ { { DISTINCT | UNIQUE } | ALL } ] select_list FROM { table_reference | join_clause | ( join_clause ) } [,{ table_reference | join_clause | (join_clause) } ] ... [ where_clause ] [ hierarchical_query_clause ] [ group_by_clause ] [ HAVING condition ] [ model_clause ]
基于此,您不能在GROUP BY子句之前使用HAVING子句.但是,如果我要在测试服务器中执行以下sql:
select department_id,count (*) from employees having count(*) > 6 group by department_id ;
它不会产生语法错误,有人可以帮忙解释一下吗?我不认为参考文档是错误的,但如果是这样,我需要一些确认.
解决方法
如
here所述:
Use the HAVING clause to restrict the groups of returned rows to those groups for which the specified condition is TRUE. If you omit this clause,then the database returns summary rows for all groups.
Specify GROUP BY and HAVING after the where_clause and hierarchical_query_clause. If you specify both GROUP BY and HAVING,then they can appear in either order.