当前位置: > 数据库 > Oracle >

oracle BREAK 命令和compute命令

时间:2015-10-11 18:15来源:linux.it.net.cn 作者:IT
一般可以用 BREAK ON column SKIP xx 来对查询结果进行排列, BREAK ON 命令的参数如下: clear breaks 清除所有的 break 定义 break on column 在该列上中断 break on row 在每一行上中断 break on Page 在每一页上中断 break on report 在每一报告上中断 s


一般可以用 BREAK ON column SKIP xx 来对查询结果进行排列,BREAK ON 命令的参数如下:
 
clear    breaks      清除所有的 break 定义
break   on   column     在该列上中断
break   on   row        在每一行上中断
break   on    Page      在每一页上中断
break   on    report    在每一报告上中断
skip     n               跳过n行
skip     page            跳过未用完的页
 
 
 
使用方法请看下面例子:
SQL> break on deptno
SQL> set pagesize 100 
SQL> select deptno,ename,sal from emp order by deptno;
 
    DEPTNO ENAME             SAL
---------- ---------- ----------
        10 CLARK            2450
           KING             5000
           MILLER           1300
        20 SMITH             800
           ADAMS            1100
           FORD             3000
           SCOTT            3000
           JONES            2975
        30 ALLEN            1600
           BLAKE            2850
           MARTIN           1250
            JAMES             950
           TURNER           1500
           WARD             1250
 
已选择14行。
 
SQL> break on deptno skip 2
SQL> select deptno,ename,sal from emp order by deptno;
 
    DEPTNO ENAME             SAL
---------- ---------- ----------
        10 CLARK            2450
           KING             5000
           MILLER           1300
 
 
        20 SMITH             800
           ADAMS            1100
           FORD             3000
           SCOTT            3000
           JONES            2975
 
 
        30 ALLEN            1600
           BLAKE            2850
           MARTIN           1250
           JAMES             950
           TURNER           1500
           WARD             1250
 
SQL> break on deptno skip page
SQL> select deptno,ename,sal from emp order by deptno;
 
    DEPTNO ENAME             SAL
---------- ---------- ----------
        10 CLARK            2450
           KING             5000
           MILLER           1300
 
    DEPTNO ENAME             SAL
---------- ---------- ----------
        20 SMITH             800
           ADAMS            1100
           FORD             3000
           SCOTT            3000
           JONES            2975
 
    DEPTNO ENAME             SAL
---------- ---------- ----------
        30 ALLEN            1600
           BLAKE            2850
           MARTIN           1250
            JAMES             950
           TURNER           1500
           WARD             1250
 
已选择14行。
 
§2.4.3   用COMPUTE命令对结果进行格式化
 
COMPUTE命令 的语法如下:
 
COMP[UTE] [function [LAB[EL] text] ...
OF {expr|column|alias} ...
ON {expr|column|alias|REPORT|ROW} ...]
 
function 可以是下面参数之一
AVG          数字类型平均值
COU[NT]     所有类型的个数
MIN[IMUM] NUMBER, CHAR,NCHAR, VARCHAR2(VARCHAR),
NVARCHAR2 (NCHARVARYI NG) 类型的最小值
MAX[IMUM] NUMBER, CHAR,NCHAR, VARCHAR2(VARCHAR),
NVARCHAR2 (NCHARVARYI NG) 类型的最大值
NUM[BER]     计算所有类型的行数
SUM           计算所有非空数字类型的总和
STD            计算数字类型的标准差
DEV[IANCE]    计算数字类型的协方差
 
LAB[EL] text
显示的字符串。用它可以替换掉字段的显示。
 
OF {expr|column|alias} ...
OF子串或表达式或别名
ON {expr|column|alias|REPORT|ROW} ...
ON子串或表达式或别名或REPORT或ROW
例子:
按照员工是 "clerk"、"analyst"、"analyst"及 "salesman"进行工资小计,并加标记"TOTAL", 则:
SQL> BREAK ON JOB SKIP 1
SQL> COMPUTE SUM LABEL ’TOTAL’ OF SAL ON JOB
SQL> SELECT JOB, ENAME, SAL
2 FROM EMP
3 WHERE JOB IN (’CLERK’, ’ANALYST’, ’SALESMAN’)
4 ORDER BY JOB, SAL;
The following output results:
JOB ENAME SAL
--------- ---------- ----------
ANALYST SCOTT 3000
FORD 3000
********* ----------
TOTAL 6000
 
CLERK SMITH 800
JAMES 950
ADAMS 1100
MILLER 1300
********* ----------
TOTAL 4150
SALESMAN WARD 1250
MARTIN 1250
TURNER 1500
ALLEN 1600
********* ----------
TOTAL 5600
 
计算工资小于 1,000 的总和:
 
SQL> COMPUTE SUM OF SAL ON REPORT
SQL> BREAK ON REPORT
SQL> COLUMN DUMMY HEADING ’’
SQL> SELECT ’ ’ DUMMY, SAL, EMPNO
2 FROM EMP
3 WHERE SAL < 1000
4 ORDER BY SAL;
 
SAL EMPNO
--- ---------- -----------
800 7369
950 7900
----------
sum 1750
 
计算平均和最大工资、部门:
 
SQL> BREAK ON DNAME SKIP 1
SQL> COMPUTE AVG LABEL ’Dept Average’ ->
MAX LABEL ’Dept Maximum’ ->
OF SAL ON DNAME
 
SQL> SELECT DNAME, ENAME, SAL
2 FROM DEPT, EMP
3 WHERE DEPT.DEPTNO = EMP.DEPTNO
4 AND DNAME IN (’ACCOUNTING’, ’SALES’)
5 ORDER BY DNAME;
 
 
DNAME ENAME SAL
-------------- ---------- ----------ACCOUNTING
CLARK 2450
KING 5000
MILLER 1300
************** ----------Dept
Average 2916.66667
 
Dept Maximum 5000
SALES ALLEN 1600
BLAKE 2850
MARTIN 1250
JAMES 950
TURNER 1500
WARD 1250
************** ----------Dept
Average 1566.66667
Dept Maximum 2850
9 rows selected.
 
计算部门10和20 的工资总和,不打印:
 
SQL> COLUMN DUMMY NOPRINT
SQL> COMPUTE SUM OF SAL ON DUMMY
SQL> BREAK ON DUMMY SKIP 1
SQL> SELECT DEPTNO DUMMY, DEPTNO, ENAME, SAL
2 FROM EMP
3 WHERE DEPTNO <= 20
4 ORDER BY DEPTNO;
 
DEPTNO ENAME SAL
---------- ---------- ----------
10 CLARK 2450
10 KING 5000
10 MILLER 1300
----------
8750
20 SMITH 800
20 ADAMS 1100
20 FORD 3000
20 SCOTT 3000
20 JONES 2975
----------
10875
8 rows selected.
 
在报告结束不打印计算工资总和:
 
SQL> COLUMN DUMMY NOPRINT
SQL> COMPUTE SUM OF SAL ON DUMMY
SQL> BREAK ON DUMMY
SQL> SELECT NULL DUMMY, DEPTNO, ENAME, SAL
2 FROM EMP
3 WHERE DEPTNO <= 20
4 ORDER BY DEPTNO;
 
DEPTNO ENAME SAL
---------- ---------- ----------
10 CLARK 2450
10 KING 5000
10 MILLER 1300
20 SMITH 800
20 ADAMS 1100
20 FORD 3000
20 SCOTT 3000
20 JONES 2975
----------
19625

8 rows selected.

 

SQL> COL USERNAME FORMAT A2
SQL>   select username from v$session where username='SYS'
   2   /
US
--
SY
S
SQL> COL USERNAME FORMAT A5
SQL> /
USERN
-----
SYS

把数据列username用5个字符的宽度显示。

(责任编辑:IT)
------分隔线----------------------------