Oracle - 为什么 HAVING 子句可以在 GROUP BY 之前
Oracle - Why HAVING clause can be before GROUP BY
Why/How HAVING clause 可以在 GROUP BY
之前
select count(1) from tableA
having id >1
group by id
order by count(1)
我知道 HAVING
子句可以没有 GROUP BY
子句,
但是在定义 GROUP BY
时,为什么 HAVING
没有像 ORDER BY
子句那样被强制如此?
Derby processes a SelectExpression in the following order:
- FROM clause
- WHERE clause
- GROUP BY (or implicit GROUP BY)
- HAVING clause
- ORDER BY clause
在所有文档中它出现在 GROUP BY
:
之后
GROUP BY WORKDEPT
HAVING MAX(SALARY) < (SELECT AVG(SALARY)
FROM EMPLOYEE
WHERE NOT WORKDEPT = EMP_COR.WORKDEPT)
编辑 Oreilly文章指出顺序不重要(但不说明原因)
The order of the GROUP BY clause and the HAVING clause in a SELECT statement is not important. We can specify the GROUP BY clause before the HAVING clause, or vice versa
我怀疑是因为 HAVING 可以没有 GROUP BY 子句然后命令不强制执行。
因为 having 子句作用于结果(过滤 select 的结果)
查询 select 可以仅基于 select 或者如果是聚合查询
结果在 GROUP BY 之后产生
这样,从语义上讲,HAVING 被放置在查询 CLAUSE 的末尾 .. 就在表示子句 (ORDER BY)
之前
Why/How HAVING clause 可以在 GROUP BY
之前select count(1) from tableA
having id >1
group by id
order by count(1)
我知道 HAVING
子句可以没有 GROUP BY
子句,
但是在定义 GROUP BY
时,为什么 HAVING
没有像 ORDER BY
子句那样被强制如此?
Derby processes a SelectExpression in the following order:
- FROM clause
- WHERE clause
- GROUP BY (or implicit GROUP BY)
- HAVING clause
- ORDER BY clause
在所有文档中它出现在 GROUP BY
:
GROUP BY WORKDEPT HAVING MAX(SALARY) < (SELECT AVG(SALARY) FROM EMPLOYEE WHERE NOT WORKDEPT = EMP_COR.WORKDEPT)
编辑 Oreilly文章指出顺序不重要(但不说明原因)
The order of the GROUP BY clause and the HAVING clause in a SELECT statement is not important. We can specify the GROUP BY clause before the HAVING clause, or vice versa
我怀疑是因为 HAVING 可以没有 GROUP BY 子句然后命令不强制执行。
因为 having 子句作用于结果(过滤 select 的结果)
查询 select 可以仅基于 select 或者如果是聚合查询
结果在 GROUP BY 之后产生
这样,从语义上讲,HAVING 被放置在查询 CLAUSE 的末尾 .. 就在表示子句 (ORDER BY)