一、BREAK语句的语法:
BRE[AK] [ON report_element [action [action ]]] ...
where report_element has the syntax {column |expr |ROW|REPORT}
and action has the syntax [SKI[P] n |[SKI[P]] PAGE] [NODUP[LICATES ]|DUP[LICATES]]
Specifies where changes occur in a report and the formatting action to perform, such as:
-
suppressing display of duplicate values for a given column
-
skipping a line each time a given column value changes
-
printing computed figures each time a given column value changes or at the end of the report.
See the COMPUTE command.
Enter BREAK with no clauses to list the current BREAK definition.
二、COMPUTE语句的语法:
COMP[UTE] [function [LAB[EL] text ] ... OF {expr | column | alias } ... ON {expr | column | alias | REPORT | ROW} ...]
In combination with the BREAK command, calculates and prints summary lines , using various standard computations on subsets of selected rows. It also lists all COMPUTE definitions. For details on how to create summaries, see Clarifying Your Report with Spacing and Summary Lines .
Terms
function ...
Represents one of the functions listed in Table 12-2, "COMPUTE Functions" . If you specify more than one function, use spaces to separate the functions.
COMPUTE command functions are always executed in the sequence AVG, COUNT, MINIMUM, MAXIMUM, NUMBER, SUM, STD, VARIANCE, regardless of their order in the COMPUTE command.
Table 12-2 COMPUTE Functions
Function | Computes | Applies to Datatypes |
---|---|---|
AVG | Average of non-null values | NUMBER |
COU[NT] | Count of non-null values | all types |
MIN[IMUM] | Minimum value | NUMBER, CHAR, NCHAR, VARCHAR2 (VARCHAR), NVARCHAR2 (NCHAR VARYING) |
MAX[IMUM] | Maximum value | NUMBER, CHAR, NCHAR, VARCHAR2 (VARCHAR), NVARCHAR2 (NCHAR VARYING) |
NUM[BER] | Count of rows | all types |
SUM | Sum of non-null values | NUMBER |
STD | Standard deviation of non-null values | NUMBER |
VAR[IANCE] | Variance of non-null values | NUMBER |
简而言之,我的理解如下:
1. break语句可以对相同的行只显示一个标题而跳过后面的标题不显示。
2. compute语句可以在某列的基础上算一些聚合数值。
以下举例说明:
三、break语句的使用
以下这段SQL如下:
with test_a as(
select to_date('20080201','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '人民币 ' as shoukfs, 100 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '信用卡 ' as shoukfs, 100 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '美金 ' as shoukfs, 100 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '人民币 ' as shoukfs, 200 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '信用卡 ' as shoukfs, 200 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '美金 ' as shoukfs, 300 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '人民币 ' as shoukfs, 100 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '信用卡 ' as shoukfs, 100 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '美金 ' as shoukfs, 100 as money from dual union all
select to_date('20080201','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '抵值券 ' as shoukfs, 100 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '人民币 ' as shoukfs, 200 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '信用卡 ' as shoukfs, 200 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '美金 ' as shoukfs, 200 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '信用卡 ' as shoukfs, 400 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'A' as yingyeyu, '美金 ' as shoukfs, 400 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '人民币 ' as shoukfs, 300 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '信用卡 ' as shoukfs, 400 as money from dual union all
select to_date('20080202','yyyymmdd') as yyyymmdd, 'B' as yingyeyu, '美金 ' as shoukfs, 400 as money from dual
)
select yyyymmdd,yingyeyu,shoukfs,sum(money)
from test_a
group by rollup(yyyymmdd,(yingyeyu,shoukfs));
1. 不适用break语句的结果
YYYYMMDD Y SHOUKFS SUM(MONEY)
------------------- - ------- ----------
2008-02-01 00:00:00 A 美金 400
2008-02-01 00:00:00 A 人民币 300
2008-02-01 00:00:00 A 信用卡 300
2008-02-01 00:00:00 B 抵值券 100
2008-02-01 00:00:00 B 美金 100
2008-02-01 00:00:00 B 人民币 100
2008-02-01 00:00:00 B 信用卡 100
2008-02-01 00:00:00 1400
2008-02-02 00:00:00 A 美金 400
2008-02-02 00:00:00 A 信用卡 400
2008-02-02 00:00:00 B 美金 600
2008-02-02 00:00:00 B 人民币 500
2008-02-02 00:00:00 B 信用卡 600
2008-02-02 00:00:00 2500
3900
2. 使用break语句后的结果
在以上那段SQL之前添加
SQL>break on yingyeyu on shoukfs
YYYYMMDD Y SHOUKFS SUM(MONEY)
------------------- - ------- ----------
2008-02-01 00:00:00 A 美金 400
2008-02-01 00:00:00 人民币 300
2008-02-01 00:00:00 信用卡 300
2008-02-01 00:00:00 B 抵值券 100
2008-02-01 00:00:00 美金 100
2008-02-01 00:00:00 人民币 100
2008-02-01 00:00:00 信用卡 100
2008-02-01 00:00:00 1400
2008-02-02 00:00:00 A 美金 400
2008-02-02 00:00:00 信用卡 400
2008-02-02 00:00:00 B 美金 600
2008-02-02 00:00:00 人民币 500
2008-02-02 00:00:00 信用卡 600
2008-02-02 00:00:00 2500
3900
总结:从执行结果可以看出明显的差异。
四、compute语句的使用。
create table test(
ID VARCHAR2(4 BYTE) NOT NULL,
First_Name VARCHAR2(10 BYTE),
Last_Name VARCHAR2(10 BYTE),
Start_Date DATE,
End_Date DATE,
Salary Number(8,2),
City VARCHAR2(10 BYTE),
Description VARCHAR2(15 BYTE)
)
/
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City,Description)
values ('01','Jason', 'Martin', to_date('19960725','YYYYMMDD'), to_date('20060725','YYYYMMDD'), 1234.56, 'Toronto', 'Programmer');
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City,Description)
values('02','Alison', 'Mathews', to_date('19760321','YYYYMMDD'), to_date('19860221','YYYYMMDD'), 6661.78, 'Vancouver','Tester');
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City,Description)
values('03','James', 'Smith', to_date('19781212','YYYYMMDD'), to_date('19900315','YYYYMMDD'), 6544.78, 'Vancouver','Tester');
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City,Description)
values('04','Celia', 'Rice', to_date('19821024','YYYYMMDD'), to_date('19990421','YYYYMMDD'), 2344.78, 'Vancouver','Manager');
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City,Description)
values('05','Robert', 'Black', to_date('19840115','YYYYMMDD'), to_date('19980808','YYYYMMDD'), 2334.78, 'Vancouver','Tester');
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City, Description)
values('06','Linda', 'Green', to_date('19870730','YYYYMMDD'), to_date('19960104','YYYYMMDD'), 4322.78,'New York', 'Tester');
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City, Description)
values('07','David', 'Larry', to_date('19901231','YYYYMMDD'), to_date('19980212','YYYYMMDD'), 7897.78,'New York', 'Manager');
insert into test(ID, First_Name, Last_Name, Start_Date,End_Date, Salary, City, Description)
values('08','James', 'Cat', to_date('19960917','YYYYMMDD'), to_date('20020415','YYYYMMDD'), 1232.78,'Vancouver', 'Tester');
SQL> select * from test order by 1;
ID FIRST_NAME LAST_NAME START_DATE END_DATE SALARY CITY DESCRIPTION
---- ---------- ---------- ------------------- ------------------- ---------- ---------- ---------------
01 Jason Martin 1996-07-25 00:00:00 2006-07-25 00:00:00 1234.56 Toronto Programmer
02 Alison Mathews 1976-03-21 00:00:00 1986-02-21 00:00:00 6661.78 Vancouver Tester
03 James Smith 1978-12-12 00:00:00 1990-03-15 00:00:00 6544.78 Vancouver Tester
04 Celia Rice 1982-10-24 00:00:00 1999-04-21 00:00:00 2344.78 Vancouver Manager
05 Robert Black 1984-01-15 00:00:00 1998-08-08 00:00:00 2334.78 Vancouver Tester
06 Linda Green 1987-07-30 00:00:00 1996-01-04 00:00:00 4322.78 New York Tester
07 David Larry 1990-12-31 00:00:00 1998-02-12 00:00:00 7897.78 New York Manager
08 James Cat 1996-09-17 00:00:00 2002-04-15 00:00:00 1232.78 Vancouver Tester
-- Multiple COMPUTEs are also allowable.
SET echo off
SET verify off --作用见五所示
BREAK ON city skip1 ON start_date DUPLICATES
COMPUTE sum max min of salary ON city
COMPUTE sum of salary ON id
SELECT id, first_name, salary, city FROM test ORDER BY city,id;
ID FIRST_NAME SALARY CITY
---- ---------- ---------- ----------
06 Linda 4322.78 New York
07 David 7897.78
---------- **********
4322.78 minimum
7897.78 maximum
12220.56 sum
01 Jason 1234.56 Toronto
---------- **********
1234.56 minimum
1234.56 maximum
1234.56 sum
02 Alison 6661.78 Vancouver
03 James 6544.78
04 Celia 2344.78
05 Robert 2334.78
08 James 1232.78
---------- **********
1232.78 minimum
6661.78 maximum
19118.9 sum
结果可以看见了吧。呵呵
五、 SET verify off
关于变量是否显示改变时的新旧状态
当在sqlplus中运行的sql语句中有替代变量(以&或&&打头)的时候,
set verify(或ver) on/off可以设置是否显示替代变量被替代前后的语句。
如:
SQL> set ver on;
SQL> select * from dual where 1=&var;
Enter value for var: 1
old 1: select * from dual where 1=&var
new 1: select * from dual where 1=1
DU
--
X
而如果设为off,则显示如下:
SQL> set ver off;
SQL> select * from dual where 1=&var;
Enter value for var: 1
DU
--
X
部分转帖自:
http://www.java2s.com/Code/Oracle/PL-SQL/BREAKONcityskip1ONstartdate.htm
http://black-star.javaeye.com/blog/436176