zoukankan      html  css  js  c++  java
  • oracle功能强大的with子句

    select * from emp ;

        EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
    1 7369 SMITH CLERK 7902 17-12月-80 800 20
    2 7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300 30
    3 7521 WARD SALESMAN 7698 22-2月 -81 1250 500 30
    4 7566 JONES MANAGER 7839 02-4月 -81 2975 20
    5 7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400 30
    6 7698 BLAKE MANAGER 7839 01-5月 -81 2850 30
    7 7782 CLARK MANAGER 7839 09-6月 -81 2450 10
    8 7788 SCOTT ANALYST 7566 19-4月 -87 3000 20
    9 7839 KING PRESIDENT 17-11月-81 5000 10

    select * from dept ;

        DEPTNO DNAME LOC
    1 10 ACCOUNTING NEW YORK
    2 20 RESEARCH DALLAS
    3 30 SALES CHICAGO
    4 40 OPERATIONS BOSTON

    功能强大的WITH子句的用法

    WITH
    dept_costs AS (
    SELECT d.dname, SUM(e.sal) AS dept_total
    FROM emp e, dept d
    WHERE e.deptno = d.deptno
    GROUP BY d.dname ),
    avg_cost AS (
    SELECT SUM(dept_total)/COUNT(*) AS dept_avg FROM dept_costs )
    SELECT * FROM dept_costs
    WHERE dept_total <
    (SELECT dept_avg FROM avg_cost)
    ORDER BY dname ;

    官方解释:

    The WITH Clause Usage Notes
    • It is used only with SELECT statements.
    • A query name is visible to all WITH element query blocks (including their subquery blocks)
    defined after it and the main query block itself (including its subquery blocks).
    • When the query name is the same as an existing table name, the parser searches from the inside
    out, the query block name takes precedence over the table name.
    • The WITH clause can hold more than one query. Each query is then separated by a comma.

  • 相关阅读:
    SSO单点登录机制
    Web应用中Service层获取request对象 | RequestContextHolder的使用
    J2EE中数据字典的使用详解
    Redis高级(事务,持久化,主从复制读写分离,以及安全设置)
    Redis的五种数据类性以及对应的操作命令
    Redis客户端与基本命令
    VMware15安装CentOS8
    用内置的库turtle来画一朵花,python3
    python之经典猜数字
    python,寻找班级里面名字最长的人
  • 原文地址:https://www.cnblogs.com/caibird2005/p/1371269.html
Copyright © 2011-2022 走看看