`

《Pro Oracle SQL》Chapter7 Advanced Grouping -- 7.2HAVING Clause

阅读更多

7.2 HAVING Clause     HAVING 子句   (page 195)

    Results generated by GROUP BY may be restricted by the criteria found in the HAVING clause.  The HAVING clause is quite versatile, resembling the WHERE clause in the conditions that may be used. Functions, operators, and subqueries may all be used in the HAVING clause. Listing 7-7 shows a query that will return all departments that have hired at least five employees since the beginning of the first full year after hiring began. 
由GROUP BY生成的结果集可以被HAVING子句中的标准所限制。HAVING子句非常“能干”,可以像条件中的WHERE子句那样使用。函数,操作符,以及子查询全都可以用于HAVING子句中。列表7-7展示一查询,返回至雇佣(工作)开始之后的第一个全年(下一年 )开始(到最后一个雇佣者受雇佣这段时间内)至少雇佣5名员工的所有部门。
    That the HAVING operation is executed after all data has been fetched can be seen as the FILTER in
step 1 of the execution plan shown in Listing 7-7.  Notice that an operator, a function, and subqueries
have all been used in the HAVING clause. 

HAVING操作执行于所有数据取出之后,可以从列表7-7所示执行计划的第一步的FILTER(筛选条件)看出。注意操作符,函数,或者子查询均可以用于HAVING子句。
Listing 7-7. HAVING Clause
  1  select /*+ gather_plan_statistics */
  2    d.dname
  3    , trunc(e.hiredate,'YYYY') hiredate 
  4    , count(empno) empcount
  5  from scott.emp e
  6  join scott.dept d on d.deptno = e.deptno
  7  group by d.dname, trunc(e.hiredate,'YYYY')
  8  having
  9    count(empno) >= 5
 10    and trunc(e.hiredate,'YYYY') between
 11      (select min(hiredate) from scott.emp)             
 12      and
 13      (select max(hiredate) from scott.emp)      --这个条件实际上总是成立
 14  order by d.dname;
 (这SQL看的怪难受的,如果习惯ORACLE风格的写法,可以看附件)
DNAME          HIREDATE              EMPCOUNT
-------------- ------------------- ----------
SALES          01/01/1981 00:00:00          6
 
1 row selected. 

PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------
| Id  | Operation                                                           | Name         | Starts   | E-Rows | A-Rows |
-----------------------------------------------------------------------------
|   0 | SELECT STATEMENT                                             |                  |      1     |                |      1      |
|*  1 |   FILTER                                                               |                  |      1     |                |      1       |
|   2 |      SORT GROUP BY                                           |                   |      1    |      1         |      6       |
|   3 |         MERGE JOIN                                               |                   |      1     |     14       |     14     |
|   4 |            TABLE ACCESS BY INDEX ROWID         | DEPT         |      1     |      4        |      4     |
|   5 |               INDEX FULL SCAN                                 | PK_DEPT     |      1      |      4       |      4      |
|*  6 |            SORT JOIN                                                |                   |      4      |     14       |     14    |
|   7 |               TABLE ACCESS FULL                            | EMP          |      1      |     14        |     14    |
|   8 |            SORT AGGREGATE                                  |                  |      1      |      1         |      1     |
|   9 |               TABLE ACCESS FULL                             | EMP         |      1       |     14        |     14    |
|  10 |           SORT AGGREGATE                                   |                  |      1      |      1         |      1     |
|  11 |              TABLE ACCESS FULL                            | EMP         |      1      |     14        |     14    |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
--------------------------------------------------- 

   1 - filter((COUNT(*)>=5 AND TRUNC(INTERNAL_FUNCTION("E"."HIREDATE"),'fmyyyy')>= AND
              TRUNC(INTERNAL_FUNCTION("E"."HIREDATE"),'fmyyyy')<=))
   6 - access("D"."DEPTNO"="E"."DEPTNO")
       filter("D"."DEPTNO"="E"."DEPTNO")
 
37 rows selected. 

 

 

 

1
1
分享到:
评论

相关推荐

    《Pro Oracle SQL》Chapter7 Advanced Grouping -- 7.5Putting CUBE To Work

    《Pro Oracle SQL》Chapter 7 Advanced Grouping 是一本关于Oracle SQL高级分组技术的专业书籍,其中7.5小节着重讲解了CUBE操作在实际工作中的应用。CUBE是SQL中的一种多维度分析工具,它允许我们在数据分组时生成...

    《Pro Oracle SQL》Chapter7 -- 7.8Extending Reports With GROUPING_ID()

    《Pro Oracle SQL》一书的第七章第7.8节主要探讨了如何通过GROUPING_ID()函数来扩展Oracle SQL报告的功能。GROUPING_ID()在数据分析和报表生成中扮演着重要角色,尤其在处理分组数据时,能提供更详细的分组信息。本...

    《Pro Oracle SQL》Chapter7 -- 7.7Extending Reports with GROUPING()

    《Pro Oracle SQL》是Oracle数据库查询的一本权威指南,其中第七章主要讲解了高级SQL查询技术。在7.7节“Extending Reports with GROUPING()”中,作者深入探讨了如何利用GROUPING函数来增强报告的功能性和灵活性。...

    《Pro Oracle SQL》Chapter7 --7.6Eliminate NULLs with the GROUPING() Function

    《Pro Oracle SQL》一书的第7章,7.6节专门讲解了如何使用GROUPING()函数来处理这个问题。GROUPING()函数是Oracle数据库提供的一种特殊函数,用于识别汇总结果中的NULL值,并且在分组操作中有着独特的应用。 NULL值...

    oracle advanced sql 高级SQL教程 ORACLE官方教材

    Review of the HAVING Clause 3-5 GROUP BY with ROLLUP and CUBE Operators 3-6 ROLLUP Operator 3-7 ROLLUP Operator Example 3-8 CUBE Operator 3-9 CUBE Operator: Example 3-10 GROUPING Function 3-11 ...

    lucene-grouping-6.6.0-API文档-中文版.zip

    赠送jar包:lucene-grouping-6.6.0.jar; 赠送原API文档:lucene-grouping-6.6.0-javadoc.jar; 赠送源代码:lucene-grouping-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-6.6.0.pom; 包含翻译后...

    Pro Oracle SQL

    Pro Oracle SQL unlocks the power of SQL in the Oracle Database—one of the most potent SQL implementations on the market today. To master it requires a three-pronged approach: learn the language ...

    lucene-grouping-7.7.0-API文档-中文版.zip

    赠送jar包:lucene-grouping-7.7.0.jar; 赠送原API文档:lucene-grouping-7.7.0-javadoc.jar; 赠送源代码:lucene-grouping-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-7.7.0.pom; 包含翻译后...

    lucene-grouping-7.2.1-API文档-中文版.zip

    赠送jar包:lucene-grouping-7.2.1.jar; 赠送原API文档:lucene-grouping-7.2.1-javadoc.jar; 赠送源代码:lucene-grouping-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-7.2.1.pom; 包含翻译后...

    lucene-grouping-7.3.1-API文档-中文版.zip

    赠送jar包:lucene-grouping-7.3.1.jar; 赠送原API文档:lucene-grouping-7.3.1-javadoc.jar; 赠送源代码:lucene-grouping-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-7.3.1.pom; 包含翻译后...

    lucene-grouping-7.2.1-API文档-中英对照版.zip

    赠送jar包:lucene-grouping-7.2.1.jar; 赠送原API文档:lucene-grouping-7.2.1-javadoc.jar; 赠送源代码:lucene-grouping-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-7.2.1.pom; 包含翻译后...

    lucene-grouping-7.7.0-API文档-中英对照版.zip

    赠送jar包:lucene-grouping-7.7.0.jar; 赠送原API文档:lucene-grouping-7.7.0-javadoc.jar; 赠送源代码:lucene-grouping-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-7.7.0.pom; 包含翻译后...

    lucene-grouping-7.3.1-API文档-中英对照版.zip

    赠送jar包:lucene-grouping-7.3.1.jar; 赠送原API文档:lucene-grouping-7.3.1-javadoc.jar; 赠送源代码:lucene-grouping-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-7.3.1.pom; 包含翻译后...

    lucene-grouping-6.6.0-API文档-中英对照版.zip

    赠送jar包:lucene-grouping-6.6.0.jar; 赠送原API文档:lucene-grouping-6.6.0-javadoc.jar; 赠送源代码:lucene-grouping-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-grouping-6.6.0.pom; 包含翻译后...

    oracle-sql-the-essential-reference

    ### Oracle SQL:核心参考知识点详解 #### 一、概述 《Oracle SQL:核心参考》(Oracle SQL: The Essential Reference)是一本由David C. Kreines撰写的书籍,于2000年由O'Reilly出版社出版。该书为Oracle SQL的...

    Beginning Transact-SQL with SQL Server 2000 and 2005 (2 of 5)

    Chapter 7 - Aggregation and Grouping Chapter 8 - Multi-Table Queries Chapter 9 - Data Transactions Chapter 10 - Advanced Queries and Scripting Chapter 11 - Full-Text Index Queries Chapter ...

    Beginning Transact-SQL with SQL Server 2000 and 2005 (1 of 5)

    Chapter 7 - Aggregation and Grouping Chapter 8 - Multi-Table Queries Chapter 9 - Data Transactions Chapter 10 - Advanced Queries and Scripting Chapter 11 - Full-Text Index Queries Chapter ...

    lucene-grouping-8.8.2.jar

    lucene-grouping-8.8.2.jar

    lucene-grouping-3.5.0.jar分组统计+分类统计插件

    lucene-grouping-3.5.0.jar分组统计+分类统计插件 分组统计+分类统计

Global site tag (gtag.js) - Google Analytics