Chapter 3 introduces and dis-cusses Oracle 10g’s analytical functions, and Chapter 4 discusses Oracle 10g’s aggregate functions that are used as analytical functions.. Appendix Aillustra
Trang 2Sikha Saha Bagui
Trang 3Advanced SQL functions in Oracle 10g / by Richard Walsh Earp
and Sikha Saha Bagui.
p cm.
Includes bibliographical references and index.
ISBN-13: 978-1-59822-021-6
ISBN-10: 1-59822-021-7 (pbk.)
1 SQL (Computer program language) 2 Oracle (Computer file).
I Bagui, Sikha, 1964- II Title.
QA76.73.S67E26 2006
CIP
© 2006, Wordware Publishing, Inc.
All Rights Reserved
2320 Los Rios Boulevard Plano, Texas 75074
No part of this book may be reproduced in any form or by
any means without permission in writing from
Wordware Publishing, Inc.
Printed in the United States of America
ISBN-13: 978-1-59822-021-6
ISBN-10: 1-59822-021-7
10 9 8 7 6 5 4 3 2 1
0601
Oracle is a registered trademark of Oracle Corporation and/or its affiliates.
Other brand names and product names mentioned in this book are trademarks or service marks of their respective companies Any omission or misuse (of any kind) of service marks or trademarks should not be regarded as intent to infringe on the property of others The publisher recognizes and respects all marks used by companies, manufacturers, and developers as a means to distinguish their products.
This book is sold as is, without warranty of any kind, either express or implied, respecting the contents of this book and any disks or programs that may accompany it, including but not limited to implied warranties for the book’s quality, performance, merchantability, or fitness for any particular purpose Neither Wordware Publishing, Inc nor its dealers or distributors shall be liable to the purchaser or any other person or entity with respect to any liability, loss, or damage caused or alleged to have been caused directly or indirectly by this book.
All inquiries for volume purchases of this book should be addressed to Wordware Publishing, Inc., at the above address Telephone inquiries may be made by calling:
Trang 6Preface xi
Acknowledgments xiii
Introduction xv
Chapter 1 Common Oracle Functions: A Function Review 1
Calling Simple SQL Functions 3
Numeric Functions 4
Common Numerical Manipulation Functions 4
Near Value Functions 7
Null Value Function 10
Log and Exponential Functions 12
Ordinary Trigonometry Functions 14
Hyperbolic Trig Functions 16
String Functions 18
The INSTR Function 18
The SUBSTR Function 20
The REPLACE Function 23
The TRIM Function 24
Date Functions 27
Chapter 2 Reporting Tools in Oracle’s SQL*Plus 31
COLUMN 32
Formatting Numbers 35
Scripts 39
Formatting Dates 41
Trang 7Chapter 3 The Analytical Functions in Oracle
(Analytical Functions I) 53
What Are Analytical Functions? 53
The Row-numbering and Ranking Functions 55
The Order in Which the Analytical Function Is Processed in the SQL Statement 65
A SELECT with Just a FROM Clause 66
A SELECT with Ordering 66
A WHERE Clause Is Added to the Statement 67
An Analytical Function Is Added to the Statement 67
A Join Is Added to the Statement 68
The Join Without the Analytical Function 69
Adding Ordering to a Joined Result 70
Adding an Analytical Function to a Query that Contains a Join (and Other WHERE Conditions) 71
The Order with GROUP BY Is Present 72
Adding Ordering to the Query Containing the GROUP BY 73
Adding an Analytical Function to the GROUP BY with ORDER BY Version 74
Changing the Final Ordering after Having Added an Analytical Function 75
Using HAVING with an Analytical Function 76
Where the Analytical Functions Can be Used in a SQL Statement 77
More Than One Analytical Function May Be Used in a Single Statement 78
The Performance Implications of Using Analytical Functions 80
Nulls and Analytical Functions 86
Partitioning with PARTITION_BY 95
A Problem that Uses ROW_NUMBER for a Solution 96
NTILE 101
RANK, PERCENT_RANK, and CUME_DIST 105
References 110
Trang 8(Analytical Functions II) 111
The Use of Aggregate Functions in SQL 111
RATIO-TO-REPORT 115
Windowing Subclauses with Physical Offsets in Aggregate Analytical Functions 120
An Expanded Example of a Physical Window 127
Displaying a Running Total Using SUM as an Analytical Function 131
UNBOUNDED FOLLOWING 134
Partitioning Aggregate Analytical Functions 135
Logical Windowing 137
The Row Comparison Functions — LEAD and LAG 143
LAG and LEAD Options 146
Chapter 5 The Use of Analytical Functions in Reporting (Analytical Functions III) 149
GROUP BY 150
Grouping at Multiple Levels 155
ROLLUP 157
CUBE 160
GROUPING with ROLLUP and CUBE 162
Chapter 6 The MODEL or SPREADSHEET Predicate in Oracle’s SQL 165
The Basic MODEL Clause 166
Rule 1 The Result Set 169
Rule 2 PARTITION BY 169
Rule 3 DIMENSION BY 170
Rule 4 MEASURES 170
RULES that Use Other Columns 174
RULES that Use Several Other Rows to Compute New Rows 178
RETURN UPDATED ROWS 183
Trang 9Aggregation on the RHS with Conditions on the
Aggregate 191
Revisiting CV with Value Offsets — Using Multiple MEASURES Values 193
Ordering of the RHS 198
AUTOMATIC versus SEQUENTIAL ORDER 202
The FOR Clause, UPDATE, and UPSERT 206
Iteration 211
A Square Root Iteration Example 214
References 221
Chapter 7 Regular Expressions: String Searching and Oracle 10g 223
A Simple Table to Illustrate an RE 225
REGEXP_INSTR 226
A Simple RE Using REGEXP_INSTR 230
Metacharacters 231
Brackets 237
Ranges (Minus Signs) 239
REGEXP_LIKE 239
Negating Carets 241
Bracketed Special Classes 243
Other Bracketed Classes 246
The Alternation Operator 247
Repetition Operators — aka “Quantifiers” 248
More Advanced Quantifier Repeat Operator Metacharacters — *, %, and ? 251
REGEXP_SUBSTR 253
Empty Strings and the ? Repetition Character 258
REGEXT_REPLACE 259
Grouping 261
The Backslash (\) 262
The Backslash as an Escape Character 263
Alternative Quoting Mechanism in Oracle 10g 264
Backreference 265
References 267
Trang 10Associative Arrays 270
The OBJECT TYPE — Column Objects 273
CREATE a TABLE with the Column Type in It 274
INSERT Values into a Table with the Column Type in It 275
Display the New Table (SELECT * and SELECT by Column Name) 275
COLUMN Formatting in SELECT 277
SELECTing Only One Column in the Composite 277
SELECT with a WHERE Clause 278
Using UPDATE with TYPEed Columns 278
Create Row Objects — REF TYPE 279
Loading the “row object” Table 281
UPDATE Data in a Table of Row Objects 283
CREATE a Table that References Our Row Objects 284
INSERT Values into a Table that Contains Row Objects (TCRO) 284
UPDATE a Table that Contains Row Objects (TCRO) 285
SELECT from the TCRO — Seeing Row Addresses 286
DEREF (Dereference) the Row Addresses 286
One-step INSERTs into a TCRO 287
SELECTing Individual Columns in TCROs 288
Deleting Referenced Rows 289
The Row Object Table and the VALUE Function 291
Creating User-defined Functions for Column Objects 292
VARRAYs 297
CREATE TYPE for VARRAYs 299
CREATE TABLE with a VARRAY 300
Trang 11The THE and VALUE Functions 306
The CAST Function 308
Using PL/SQL to Create Functions to Access Elements 311
Creating User-defined Functions for VARRAYs 320
Nested Tables 324
Summary 334
References 335
Chapter 9 SQL and XML 337
What Is XML? 338
Displaying XML in a Browser 342
SQL to XML 344
Generating XML from “Ordinary” Tables 344
XML to SQL 347
References 355
Appendix A String Functions 357
Appendix B Statistical Functions 371
Index 392
Trang 12Why This Book?
Oracle®10g has introduced new features into its
reper-toire of SQL instructions that make database queriesmore versatile When programmers use SQL in Oracle,they inevitably look for easier and new ways to handlequeries What is needed is a way to introduce SQL
users to the new features of Oracle 10g concisely and
systematically so that database programmers can takefull advantage of the newer capabilities This bookhopes to meet this need by exploring some commonnew SQL features Each chapter includes numerousworking examples, and Oracle users can run theseexamples as they read and work through the book
Also, many books on Oracle 10g present the language
syntax alone with no in-depth explanation, analysis, orexamples In this book, we present not only the syntaxfor new features and functions, but also a thoroughclarification and breakdown of the different functions,along with examples of ways they can and should beused
Audience and Coverage
This book is meant to be used by Oracle professionals
Trang 13database classes in schools It is meant for those iar with SQL programming since most of the topicspresent not only the syntax, queries, and answers, butalso have an analytical programming perspective tothem This book will allow the Oracle user to use SQL
famil-in new and excitfamil-ing ways
This book contains nine chapters It begins byreviewing some of the common SQL functions andtechniques to help transition into the newer tools of
Oracle 10g Chapter 1 reviews common Oracle
func-tions Chapter 2 covers some common reporting tools
in Oracle’s SQL*Plus Chapter 3 introduces and
dis-cusses Oracle 10g’s analytical functions, and Chapter 4 discusses Oracle 10g’s aggregate functions that are
used as analytical functions Chapter 5 looks at the use
of analytical functions in reporting — for example, theuse of GROUP BY, ROLLUP, and CUBE Chapter 6discusses the MODEL or SPREADSHEET predicate
in Oracle’s SQL Chapter 7 covers the new regularexpressions and string functions Chapter 8 discusses
collections and object-oriented features of Oracle 10g.
Chapter 9 introduces by example the bridges betweenSQL and XML, one of the most important topics Ora-cle professionals are expected to know today
This book also has two appendices Appendix Aillustrates string functions with examples, and Appen-dix B gives examples of some important statistical
functions available in Oracle 10g.
Overall, this book explores advanced new features
of SQL in Oracle 10g from a programmer’s perspective.
The book can be considered a starting point forresearch using some of the advanced topics since thesubjects are discussed at length with examples andsample outputs Query development is approachedfrom a logical standpoint, and in many areas perfor-mance implications of the queries are also discussed
Trang 14Our special thanks to the staff at Wordware lishing, especially Wes Beckwith, Beth Kohler, MarthaMcCuller, and Denise McEvoy.
Pub-We would also like to thank President JohnCavanaugh, Dean Jane Halonen, and Provost SandraFlake for their inspiration, encouragement, support,and true leadership We would also like to express ourgratitude to Dr Wes Little on the same endeavor Oursincere thanks also goes to Dr Ed Rodgers for his con-tinuing support and encouragement throughout theyears We also appreciate Dr Leonard Ter Haar, chair
of the computer science department, for his advice,guidance, and support, and encouraging us to completethis book Last, but not least, we would like to thankour fellow faculty members Dr Jim Bezdek and Dr.Norman Wilde for their continuous support andencouragement
Trang 16With the advent of new features added to SQL in
Ora-cle 10g, we thought that some collection of material
related to the newer query mechanisms was in order.Hence, in this book we have gathered some useful new
tools into a set of topics for exploiting Oracle 10g’s
SQL We have also briefly reviewed some older toolsthat will help transition to the new material
This book mainly addresses advanced topics in
SQL with a focus on SQL functions for Oracle 10g The
functions and methods we cover include the analyticalfunctions, MODEL statements, regular expressions,and object-oriented/collection structures We alsointroduce and give examples of the SQL/XML bridges
as XML is a newer and common method of transferringdata from user to user We rely heavily on examples, asmost SQL programmers can and do adapt examples toother problems quickly
Prerequisites
Some knowledge of SQL is assumed before beginningthis study, as this book is not meant to be a SQLprimer More specifically, some knowledge of Oraclefunctions is desirable, although some common func-tions are reviewed in Chapter 1 Functions have been
Trang 17include some unique functions as well Many peoplenow use XML to capture and move data; examples ofmoving data from SQL*Plus to and from XML are alsocovered.
Some knowledge of spreadsheets is helpful indigesting this material The analytical functions andMODEL statements provide convenient ways to dis-play and use data in a manner similar to a spreadsheet.While these functions are far more than simply displaymechanisms, often reporting/formatting functions areused in conjunction with analytical functions Wereview some common reporting functions in Chapter 2
Our Approach to SQL
In addition to a basic knowledge of SQL, we will callattention to “our way” of developing queries in SQL.The way we develop queries in SQL is often by begin-ning with a simple command and then building upon ituntil the answer is found There are different
approaches to building queries in SQL as in any otherlanguage One way is to build for a result using logical,intermediate steps A second way to build SQL queries
is for performance In a real-world environment withlarge tables, performance usually becomes an issue onoften-run commands Even in the development of que-ries, performance issues may arise
The way this material is approached is less fromthe performance perspective and more from the logical,developmental viewpoint Once a result is obtained, ifthe query is to be rerun, it is most appropriate to tunethe query for performance by examining the way it wasdone and perhaps look for alternatives, e.g., joins ver-sus subqueries
To develop queries, we will often find a result set
Trang 18examine intermediate results If the intermediateresult is faulty, then we correct and refine before wemove on One should always be suspicious of intermedi-ate results by asking questions like, “Does this resultmake sense?”, “How can we have that many rows?”, or
“How many rows did you expect?” When we are fied with the result we have produced, we use theresult in a virtual table to attain the next level
satis-For example, consider this query:
SELECT class, COUNT(*)
FROM students
GROUP BY class
Having studied this result, we might use it in a virtualtable for another query We can wrap our workingquery in parentheses (hence making it a virtual view)and then query it like this:
In writing queries, we often use aliasing Somemight argue that we overuse aliases, but we believethat it makes a query more meaningful, easier todebug, and more available for change in the future Aswell, in deference to precedence rules and defaults,
Trang 20Chapter 1
Common Oracle Functions: A
one-to-one function By one-to-one we mean that if onepositive number is furnished, then one square root
Trang 21If we show the square root function algebraically asSQRT, the resulting number as “Answer,” the equalsign as meaning “is assigned to,” and the number to beoperated on as “original_value,” then the function could
be written like this:
Answer = SQRT(original_value)
where original_value is a positive number.
In algebra, the allowable values of original_value
are called the domain of the function, which in this case
is the set of non-negative numbers Answer is called the range of the function Original_value in this exam-
ple is called the argument of the function SQRT
Oftentimes in computer situations, there is also anupper limit on the domain and range, but theoretically,there is no upper limit in algebra The lower limit onthe domain is zero as the square root of negative num-bers is undefined unless one ventures into the area ofcomplex numbers, which is beyond the scope of thisdiscussion
Almost any programming language uses functionssimilar to those found on calculators In fact, most pro-gramming languages go far beyond the calculatorfunctions
Oracle’s SQL contains a rich variety of functions
We can categorize Oracle’s SQL functions into simpleSQL functions, numeric functions, statistical functions,string functions, and date functions In this chapter, weselectively illustrate several functions in each of thesecategories We start by discussing simple SQL
functions
Trang 22Calling Simple SQL Functions
Oracle has a large number of simple functions ever a value is used directly or computed in a SQLstatement, a simple SQL function may be used Toillustrate the above square root function, suppose that
Wher-a tWher-able nWher-amed MeWher-asurement contWher-ained Wher-a series ofnumeric measured values like this:
Subject Value First 35.78 Second 22.22 Third 55.55
We could display the table with this SQL query:
SELECT * FROM measurement
eNote: We will not use semicolons at the end of SQL
statement illustrations; to run these statements in Oracle from the command line, a semicolon must be added.
From the editor, a slash (/) is added to execute the ment and no semicolon is used.
state-We could also generate the same result set with thisSQL query:
SELECT subject, value FROM measurement
Using the latter query, and adding a square root tion to the result set, the SQL query would look like
Trang 23func-This would give the following result:
SUBJECT VALUE SQRT(VALUE) - - - First 35.78 5.98163857 Second 22.22 4.7138095 Third 55.55 7.45318724
Numeric Functions
In this section we present and discuss several usefulnumeric functions, which we divide into the followingcategories: common numerical manipulation functions,near value functions, null value functions, log and expo-nential functions, ordinary trigonometry functions, andhyperbolic trignometrical functions
Common Numerical Manipulation Functions
These are functions that are commonly used in cal manipulations Examples of common numericalmanipulation functions include:
numeri-ABS — Returns the absolute value of a number orvalue
SQRT — Returns the square root of a number orvalue
MOD — Returns the remainder of n/m where both
n and m are integers.
SIGN — Returns 1 if the argument is positive; –1 ifthe argument is negative; and 0 if the argument isnegative
Trang 24Next we present a discussion on the use of these mon numerical manipulation functions Suppose we had
com-a tcom-able thcom-at looked like this:
DESC function_illustrator
Which would give:
Name Null? Type
- - LINENO NUMBER(2)
Trang 25Had we tried to include SQRT in this example ourquery would look like this:
SELECT lineno, value, ABS(value), SQRT(value), SIGN(value), MOD(lineno,2)
Functions can be nested; we can have a functionoperate on the value produced by another function Toillustrate a nested function we can use the ABS func-tion to ensure that the SQRT function sees only apositive domain The following query handles both pos-itive and negative numbers:
Trang 26SELECT lineno, value, ABS(value), SQRT(ABS(value))
FROM function_illustrator
ORDER BY lineno
This would give us:
LINENO VALUE ABS(VALUE) SQRT(ABS(VALUE))
Near Value Functions
These are functions that produce values near what youare looking for Examples of near value functionsinclude:
CEIL — Returns the ceiling value (next highestinteger above a number)
FLOOR — Returns the floor value (next lowestinteger below number)
TRUNC — Returns the truncated value (removesdecimal part of a number, precision adjustable).ROUND — Returns the number rounded to near-est value (precision adjustable)
Next we present illustrations and a discussion on theuse of these near value functions The near value func-
Trang 27SELECT lineno, value, ROUND(value), TRUNC(value), CEIL(value), FLOOR(value)
FROM function_illustrator
You will get:
LINENO VALUE ROUND(VALUE) TRUNC(VALUE) CEIL(VALUE) FLOOR(VALUE) - - - - - -
high-TRUNC simply removes decimal values
CEIL returns the next highest integer valueregardless of the fraction In this case, “next highest”refers to the actual higher number whether positive ornegative
FLOOR returns the integer below the number,again regardless of whether positive or negative.The ROUND and TRUNC functions also may have
a second argument to handle precision, which heremeans the distance to the right of the decimal point
So, the following query:
SELECT lineno, value, ROUND(value,1), TRUNC(value,1) FROM function_illustrator
Trang 28The value 3.88, when viewed from one place to the right
of the decimal point, rounds up to 3.9 and truncates to3.8
The second argument defaults to 0 as previouslyillustrated The following query may be compared withprevious versions, which have no second argument:
SELECT lineno, value, ROUND(value,0), TRUNC(value,0)
FROM function_illustrator
Which will give:
LINENO VALUE ROUND(VALUE,0) TRUNC(VALUE,0)
Trang 29Which will give:
LINENO VALUE ROUND(VALUE,-1) TRUNC(VALUE,-1) - - - -
or greater will be rounded up to 10
Null Value Function
This function is used if there are null values The nullvalue function is:
NVL — Returns a substitute (some other value) if
a value is null
NVL takes two arguments The first argument is thefield or attribute that you would like to look for the nullvalue in, and the second argument is the value that youwant to replace the null value by For example, in thestatement “NVL(value, 10)”, we are looking for nullvalues in the “value” column, and would like to replacethe null value in the “value” column by 10
To illustrate the null value function through anexample, let’s insert another row into our Function_illustrator table, as follows:
INSERT INTO function_illustrator values (7, NULL)
Trang 30Now, if you type:
Note that lineno 7 has a null value To give a value of 10
to value for lineno = 7, type:
SELECT lineno, NVL(value, 10)
Trang 31in the SELECT statement (for example, if you aredoing some calculations).
Log and Exponential Functions
SQL’s log and exponential functions include:
LN — Returns natural logs, that is, logs withrespect to base e
LOG — Returns base 10 log
EXP — Returns e raised to a value
POWER — Returns value raised to some tial power
exponen-To illustrate these functions, look at the followingexamples:
Example 1: Using the LN function:
SELECT LN(value) FROM function_illustrator WHERE lineno = 2
This will give:
LN(VALUE) - 1.35583515
Example 2: Using the LOG function:
The LOG function requires two arguments The firstargument is the base of the log, and the second argu-ment is the number that you want to take the log of Inthe following example, we are taking the log of 2, base
value.
Trang 33SELECT POWER(value,2) FROM function_illustrator WHERE lineno = 0
Which gives:
POWER(VALUE,2) -
81
Ordinary Trigonometry Functions
SQL’s ordinary trigonometry functions include:
SIN — Returns the sine of a value
COS — Returns the cosine of a value
TAN — Returns the tangent of a value
The SIN, COS, and TAN functions take arguments inradians where,
radians = (angle * 2 * 3.1416 / 360)
To illustrate the use of the ordinary trigonometricfunctions, let’s suppose we have a table called Trig withthe following description:
DESC trig
Will give:
Name Null? Type - - - VALUE1 NUMBER(3)
VALUE2 NUMBER(3) VALUE3 NUMBER(3)
Trang 35TAN(VALUE1*2*3.1416/360) -
.577351902
Hyperbolic Trig Functions
SQL’s hyperbolic trigonometric functions include:SINH — Returns the hyperbolic sine of a value.COSH — Returns the hyperbolic cosine of a value.TANH — Returns the hyperbolic tangent of avalue
These hyperbolic trigonometric functions also takearguments in radians where,
Gives:
SINH(VALUE1*2*3.1416/360) -
.54785487
Trang 36Example 2: Using the COSH function to find thehyperbolic cosine of 30 degrees:
Trang 37String Functions
A host of string functions are available in Oracle
String functions refer to alphanumeric character
strings Among the most common string functions areINSTR, SUBSTR, REPLACE, and TRIM Here wepresent and discuss these string functions INSTR,SUBSTR, and REPLACE have analogs in Chapter 7,
“Regular Expressions: String Searching and Oracle
10g.”
The INSTR Function
INSTR (“in-string”) is a function used to find patterns
in strings By patterns we mean a series of meric characters The general syntax of INSTR is:
alphanu-INSTR (string to search, search pattern [, start [, occurrence]])
The arguments within brackets ([]) are optional Wewill illustrate each argument with examples INSTR
returns a location within the string where search
pat-tern begins Here are some examples of the use of the
3
Trang 38The first character of string to search is numbered 1 Since “is” is the search pattern, it is found in string to
search at position 3 If we had chosen to look for the
second occurrence of “is,” the query would look likethis:
SELECT INSTR('This is a test','is',1,2)
string to search If a fourth argument is desired, then
the third argument is mandatory
If search pattern is not in the string, the INSTR
function returns 0, as shown by the query below:
SELECT INSTR('This is a test','abc',1,2)
Trang 39The SUBSTR Function
The SUBSTR function returns part of a string Thegeneral syntax of the function is as follows:
SUBSTR(original string, begin [,how far])
An original string is to be dissected beginning at the
begin character If no how far amount is specified, then
the rest of the string from the begin point is retrieved.
If begin is negative, then retrieval occurs from the right-hand side of original string Below is an example:
SELECT SUBSTR('My address is 123 Fourth St.',1,12) FROM dual
Which would give:
SUBSTR('MYAD -
My address i
Here, the first 12 characters are returned from
origi-nal string The first 12 characters are specified since begin is 1 and how far is 12 Notice that blanks count as
characters Look at the following query:
SELECT SUBSTR('My address is 123 Fourth St.',5,12) From dual
This would give:
SUBSTR('MYAD - ddress is 12
In this case, the retrieval begins at position 5 and againgoes for 12 characters
Trang 40Here is an example of a retrieval with no third
argument, meaning it starts at begin and retrieves the
rest of the string:
SELECT SUBSTR('My address is 123 Fourth St.',6)
SUBSTR may also retrieve from the right-hand side of
original string, as shown below:
SELECT SUBSTR('My address is 123 Fourth St.',-9,5)
Often in string handling, SUBSTR and INSTR areused together For example, if we had a series ofnames in last name, first name format, e.g., “Harrison,John Edward,” and wanted to retrieve first and middlenames, we could use the comma and space to find theend of the last name This is particularly useful sincethe last name is of unknown length and we rely only on