Documente Academic
Documente Profesional
Documente Cultură
Graeme Birchall
29-Nov-2006
DB2 9 Cookbook
Introduction
These overhead slides can be used in conjunction my SQL Cookbook to teach basic DB2 SQL to a small audience. Based on my own teaching experiences, the class should take about two days to complete, but if might be better to do it over three days. There are no exercises. As much as possible, I have tried to make every slide self-contained. Thus, it should have some sample SQL, the input data, and the answer. And with very few exceptions, I've laid out the slides in the same sequence as the examples in the related SQL Cookbook.
Author
Author: Email: Web: Title: Date: Graeme Birchall Graeme_Birchall@verizon.net http://mysite.verizon.net/Graeme_Birchall/ DB2 9 SQL Cookbook 29-Nov-2006
Introduction to SQL
DB2 9 Cookbook
Document History
Course Editions
This document is a work in progress.
2000-12-04: 2001-01-03: 2001-02-06: 2001-04-11: 2001-11-21: 2001-12-07: 2002-03-11: 2002-08-20: 2003-01-02: 2003-07-11: 2003-09-04: 2003-12-31: 2004-07-21: 2004-11-03: 2006-09-13: 2006-11-06: 2006-11-29:
Initial edition. Included chapters on: with SQL, and Quirks in SQL. Two new chapters added:
Added new chapter on Scalar Functions (selected). Replace chapter on sub-queries. Update for DB2 V7.2. Also added chapter on Identity Columns. Added chapter on OLAP Functions and GROUP BY. Minor changes, mostly to section on precedence rules.. V8.1 (beta) edition. V8.1 (final) edition. Few minor updates. Tidied up some SQL, and added section on MERGE. Added section of SELECT from DML. Reissued for V8.2. Reissued for V9.1. Tidied up some code. No major changes. No major changes. Lots of changes in lots of places. Very minor changes.
Removed all references to "UDB". Changed all "DB2 V9" to "DB2 9".
Please Share
The more people that use this material, the more inclined I am to do more work keeping it up to date. If you like what you have, please tell other people about it.
Introduction to SQL
DB2 9 Cookbook
Table of Contents
Introduction to SQL .......................................................................................................................... 5 DML Statements.............................................................................................................................. 48 Compound SQL .............................................................................................................................. 63 Column Functions .......................................................................................................................... 75 OLAP Functions.............................................................................................................................. 92 Scalar Functions........................................................................................................................... 117 User-Defined Functions ............................................................................................................... 138 Order By, Group By, and Having................................................................................................. 151 Joins .............................................................................................................................................. 165 Sub-Query ..................................................................................................................................... 189 Union, Intersect, and Except ....................................................................................................... 219 Union, Intersect, and Except ....................................................................................................... 228 Identity Columns and Sequences ............................................................................................... 237 Temporary Tables......................................................................................................................... 259 Recursive SQL .............................................................................................................................. 278 Fun with SQL................................................................................................................................. 302 Quirks in SQL................................................................................................................................ 323
Introduction to SQL
DB2 9 Cookbook
Introduction to SQL
Introduction to SQL
DB2 9 Cookbook
DISTINCT * Resume , FROM table name view name Mandatory Optional WHERE expression and / or Repeat End
Notes
Upper case text is a SQL keyword Italic text is a placeholder, or explained elsewhere Backward arrows enable one to repeat phrases Branch line going above main line is default Branch line going below main line is optional
Introduction to SQL
DB2 9 Cookbook
Statement Delimiters
--#SET DELIMITER ! SELECT name FROM staff WHERE id = 10! --#SET DELIMITER ; SELECT name FROM staff WHERE id = 20;
Notes
DB2 does not come with an "official" statement delimiter Most people, most of the time, use the semi-colon The semi-colon does not work for compound SQL In DB2BATCH, you can set the value (see above)
Introduction to SQL
DB2 9 Cookbook
Table Definitions
CREATE TABLE employee (empno CHARACTER (00006) ,firstnme VARCHAR (00012) ,midinit CHARACTER (00001) ,lastname VARCHAR (00015) ,workdept CHARACTER (00003) ,phoneno CHARACTER (00004) ,hiredate DATE ,job CHARACTER (00008) ,edlevel SMALLINT ,sex CHARACTER (00001) ,birthdate DATE ,salary DECIMAL (00009,02) ,bonus DECIMAL (00009,02) ,comm DECIMAL (00009,02) ) DATA CAPTURE NONE; NOT NOT NOT NOT NULL NULL NULL NULL
NOT NULL
Introduction to SQL
DB2 9 Cookbook
View Definitions
Queries Data Table
CREATE VIEW employee_view AS SELECT a.empno, a.firstnme, a.salary, a.workdept FROM employee a WHERE a.salary >= (SELECT AVG(b.salary) FROM employee b WHERE a.workdept = b.workdept);
Introduction to SQL
DB2 9 Cookbook
Introduction to SQL
10
DB2 9 Cookbook
UPDATE ====== no no no no no no yes yes yes yes yes yes yes yes yes yes yes yes no no no no no yes no yes
DATA-TYPE ============= VARCHAR(255) VARCHAR(255) VARCHAR(255) VARCHAR(255) DATE INTEGER VARCHAR(18) CHAR(5) VARCHAR(254) CHAR(8) CHAR(2) INTEGER VARCHAR(254) VARCHAR(4096) VARCHAR(254) INTEGER DECIMAL(20,6) VARCHAR(128) VARCHAR(18) TIME TIMESTAMP DECIMAL(6,0) VARCHAR(128) VARCHAR(128) VARCHAR(128) VARCHAR(128)
11
DB2 9 Cookbook
Example
CREATE DISTINCT TYPE USA_DOLLARS AS DECIMAL(9,2) WITH COMPARISONS; CREATE DISTINCT TYPE EUR_DOLLARS AS DECIMAL(9,2) WITH COMPARISONS;
Notes
User-defined data types Based on existing DB2 data types Used to limit certain types of usage (e.g. addition) Creation of distinct type also creates two functions: To convert data from base type to distinct type To convert data from distinct type to base type Distinct type supports following comparison operators: =, <>, <, <=, >, and >=
Introduction to SQL
12
DB2 9 Cookbook
Example
CREATE DISTINCT TYPE USA_DOLLARS AS DECIMAL(9,2) WITH COMPARISONS; CREATE DISTINCT TYPE EUR_DOLLARS AS DECIMAL(9,2) WITH COMPARISONS;
Usage
CREATE TABLE customer (id INTEGER ,fname VARCHAR(00010) ,lname VARCHAR(00015) ,date_of_birth DATE ,citizenship CHAR(03) ,usa_sales USA_DOLLARS ,eur_sales EUR_DOLLARS ,sales_office# SMALLINT ,last_updated TIMESTAMP ,PRIMARY KEY(id)); NOT NULL NOT NULL WITH DEFAULT '' NOT NULL WITH DEFAULT ''
Introduction to SQL
13
DB2 9 Cookbook
ORDER BY clause
SELECT Statement
SELECT , an item * , table name view name alias name ( full select ) expression and /or
FROM
correlation name AS
WHERE
Introduction to SQL
14
DB2 9 Cookbook
Notes
SELECT "*" implies select all columns
Introduction to SQL
15
DB2 9 Cookbook
Introduction to SQL
16
DB2 9 Cookbook
Notes
Stops fetching after "n" rows If no ORDER BY, gets first "n" random rows If ORDER BY not on unique field, also random results See other pages for more on this subject
Introduction to SQL
17
DB2 9 Cookbook
Notes
There is no unique index on YEARS field
Introduction to SQL
18
DB2 9 Cookbook
Correlation Name
In Nested Table Expression
SELECT FROM a.empno ,a.lastname employee A ,(SELECT MAX(empno)AS empno FROM employee) AS b a.empno = b.empno; ANSWER ================= EMPNO LASTNAME ------ ---------000340 GOUNOT
WHERE
In Join
SELECT a.empno ,a.lastname ,b.deptno AS dept employee A ,department b a.workdept = b.deptno a.job <> 'SALESREP' b.deptname = 'OPERATIONS' a.sex IN ('M','F') b.location IS NULL BY 1; ANSWER ====================== EMPNO LASTNAME DEPT ------ ---------- ---000090 HENDERSON E11 000280 SCHNEIDER E11 000290 PARKER E11 000300 SMITH E11 000310 SETRIGHT E11
Notes
If same table is accessed twice, correlation names are essential
Introduction to SQL
19
DB2 9 Cookbook
VIEW Definition
CREATE view emp2 AS SELECT empno AS e_num ,midinit AS "m int" ,phoneno AS "..." FROM employee; SELECT * FROM emp2 WHERE "..." = '3978';
Notes
Renamed fields can be referred to in ORDER BY, and outside of full-select Renamed fields cannot be referred to in GROUP BY, WHERE, HAVING
Introduction to SQL
20
DB2 9 Cookbook
Null Values
AVG of Field Containing Null Values
SELECT FROM WHERE AVG(comm) AS a1 ,SUM(comm) / COUNT(*) AS a2 staff id < 100; ANSWER =============== A1 A2 ------- -----796.025 530.68
Introduction to SQL
21
DB2 9 Cookbook
FROM WHERE
Double-quotes
SELECT id AS ,dept AS ,years AS ,'ABC' AS ,'"' AS FROM staff s WHERE id < 40 ORDER BY "USER ID"; "USER ID" "D#" "#Y" "'TXT'" """quote"" fld" ANSWER =============================== USER ID D# #Y 'TXT' "quote" fld ------- -- -- ----- ----------10 20 7 ABC " 20 20 8 ABC " 30 38 5 ABC "
Notes
Use quotes to bound text Use double-quotes to give non-standard names to objects Two quotes or double-quotes together represent the value
Introduction to SQL
22
DB2 9 Cookbook
Basic Predicate
Syntax
expresion NOT = <> < > <= >= expression
Sample SQL
SELECT FROM WHERE AND NOT AND NOT AND AND AND AND NOT ORDER BY id, job, dept staff job = 'Mgr' job <> 'Mgr' job = 'Sales' id <> 100 id >= 0 id <= 150 dept = 50 id; ANSWER =============== ID JOB DEPT --- ---- ---10 Mgr 20 30 Mgr 38 50 Mgr 15 140 Mgr 51
Introduction to SQL
23
DB2 9 Cookbook
Quantified Predicate (1 of 3)
Syntax
expression NOT = <> < > <= >= ) = SOME ANY ALL ( fullselect )
, ( expression
SOME ANY
Notes
Basic Predicate: Quantified Predicate: Compares two values Compares "n" values with a collection of values (i.e. uses sub-query)
Introduction to SQL
24
DB2 9 Cookbook
Quantified Predicate (2 of 3)
Two Single-value Checks
SELECT FROM WHERE AND ORDER BY id, job staff job = ANY (SELECT job FROM staff) id <= ALL (SELECT id FROM staff) id; ANSWER ======== ID JOB --- ---10 Mgr
Multi-value Check
SELECT FROM WHERE id, dept, job staff (id,dept) = ANY (SELECT dept, id FROM staff) ORDER BY 1; ANSWER ============== ID DEPT JOB --- ---- ----20 20 Sales
Introduction to SQL
25
DB2 9 Cookbook
Quantified Predicate (3 of 3)
, ( NOT expression ) = ( , expression )
Multi-value Check, 1 of 2
SELECT FROM WHERE OR OR ORDER BY id, dept, job staff (id,dept) = (30,28) (id,years) = (90, 7) (dept,job) = (38,'Mgr') 1; ANSWER =========== ID DEPT JOB -- ---- --30 38 Mgr
Multi-value Check, 2 of 2
SELECT FROM WHERE OR OR ORDER BY id, dept, job staff (id = 30 AND (id = 90 AND (dept = 38 AND 1; ANSWER =========== ID DEPT JOB -- ---- --30 38 Mgr
Introduction to SQL
26
DB2 9 Cookbook
BETWEEN Predicate
Syntax
exprsn. NOT NOT BETWEEN low val. AND high val.
Sample SQL
SELECT id, job FROM staff WHERE id BETWEEN 10 AND 30 AND id NOT BETWEEN 30 AND 10 AND NOT id NOT BETWEEN 10 AND 30 ORDER BY id; ANSWER ========= ID JOB --- ----10 Mgr 20 Sales 30 Mgr
Notes
Always check between LOW and HIGH
Introduction to SQL
27
DB2 9 Cookbook
EXISTS Predicate
Syntax
EXISTS NOT ( fullselect )
Sample SQL SELECT id, job FROM staff a WHERE EXISTS (SELECT * FROM staff b WHERE b.id = a.id AND b.id < 50) ORDER BY id; ANSWER ========= ID JOB --- ----10 Mgr 20 Sales 30 Mgr 40 Sales
Notes
See Sub-Query chapter for more details
Introduction to SQL
28
DB2 9 Cookbook
IN Predicate
exprsn. NOT , ( expression ) NOT NOT IN ( fullselect ) ( expression IN , expression
( fullselect )
Single Value
SELECT FROM WHERE AND id, job staff a id IN (10,20,30) id IN (SELECT id FROM staff) AND id NOT IN 99 ORDER BY id; ANSWER ========= ID JOB --- ----10 Mgr 20 Sales 30 Mgr
Multiple Values
SELECT FROM WHERE empno, lastname employee (empno, 'AD3113') IN (SELECT empno, projno FROM emp_act WHERE emptime > 0.5) ORDER BY 1; ANSWER =============== EMPNO LASTNAME ------ ------000260 JOHNSON 000270 PEREZ
Introduction to SQL
29
DB2 9 Cookbook
LIKE Predicate (1 of 2)
Syntax
exprsn. NOT NOT LIKE pattern ESCAPE pattern
LIKE Usage
SELECT id, name FROM staff WHERE name LIKE 'S%n' OR name LIKE '_a_a%' OR name LIKE '%r_%a' ORDER BY id; ANSWER ============== ID NAME --- --------130 Yamaguchi 200 Scoutten
'AB%' 'A/%C' ESCAPE '/' 'A\_C' ESCAPE '\' 'A$_$$' ESCAPE '$';
Introduction to SQL
30
DB2 9 Cookbook
LIKE Predicate (2 of 2)
LIKE STATEMENT TEXT =========================== LIKE 'AB%' LIKE 'AB%' ESCAPE '+' LIKE 'AB+%' ESCAPE '+' LIKE 'AB++' ESCAPE '+' LIKE 'AB+%%' ESCAPE '+' LIKE 'AB++%' ESCAPE '+' LIKE 'AB+++%' ESCAPE '+' LIKE 'AB+++%%' ESCAPE '+' LIKE 'AB+%+%%' ESCAPE '+' LIKE 'AB++++' ESCAPE '+' LIKE 'AB+++++%' ESCAPE '+' LIKE 'AB++++%' ESCAPE '+' LIKE 'AB+%++%' ESCAPE '+' WHAT VALUES MATCH ====================== Finds AB, any string Finds AB, any string Finds AB% Finds AB+ Finds AB%, any string Finds AB+, any string Finds AB+% Finds AB+%, any string Finds AB%%, any string Finds AB++ Finds AB++% Finds AB++, any string Finds AB%+, any string
Notes
Use escape character to search for "%" or "_" Repeat escape character to search for itself
Introduction to SQL
31
DB2 9 Cookbook
NULL Predicate
exprsn. NOT IS NOT NULL
Sample SQL
SELECT id, comm FROM staff WHERE id < 100 AND id IS NOT NULL AND comm IS NULL AND NOT comm IS NOT NULL ORDER BY id; ANSWER ========= ID COMM --- ---10 30 50 -
Notes
Can't use "=" to check for nulls Use COALESCE function to convert null values to something else
Introduction to SQL
32
DB2 9 Cookbook
Precedence Rules (1 of 2)
Basic Arithmetic
Example: 555 + ^ 5th -22 / (12 - 3) * 66 ^ ^ ^ ^ 2nd 3rd 1st 4th ANSWER ====== 423
Integer Example
SELECT (12 , -22 / (12 , -22 / (12 ,555 + -22 / (12 sysibm.sysdummy1; 3) 3) 3) * 66 3) * 66 AS AS AS AS int1 int2 int3 int4 => => => => 9 -2 -132 432
FROM
Decimal Example
SELECT (12.0 , -22 / (12.0 , -22 / (12.0 ,555 + -22 / (12.0 sysibm.sysdummy1; 3) 3) 3) * 66 3) * 66 AS AS AS AS dec1 dec2 dec3 dec4 => => => => 9.0 -2.4 -161.3 393.6
FROM
Notes
MULTIPLICATION and DIVISION before ADDITION and SUBTRACTION INTEGER arithmetic truncates fractional results
Introduction to SQL 33
DB2 9 Cookbook
Precedence Rules (2 of 2)
And vs. OR
SELECT FROM WHERE AND OR ORDER BY * table1 col1 = 'C' col1 >= 'A' col2 >= 'AA' col1; ANSWER>> COL1 col2 ---- ---A AA B BB C CC TABLE1 +---------+ |COL1|COL2| |----|----| |A |AA | |B |BB | |C |CC | +---------+
SELECT * FROM table1 WHERE (col1 = 'C' AND col1 >= 'A') OR col2 >= 'AA' ORDER BY col1; SELECT * FROM table1 WHERE col1 = 'C' AND (col1 >= 'A' OR col2 >= 'AA') ORDER BY col1;
ANSWER>>
COL1 ---A B C
col2 ---AA BB CC
ANSWER>>
Notes
AND operations done before OR operations Parenthesis can be used to alter precedence
Introduction to SQL 34
DB2 9 Cookbook
CAST Expression (1 of 2)
CAST ( expression NULL parameter maker AS data-type )
Notes
Converts one data type to another Defines field type of temporary field
Introduction to SQL
35
DB2 9 Cookbook
CAST Expression (2 of 2)
Truncate Char field
SELECT id ,job ,CAST(job AS CHAR(3)) AS job2 FROM staff WHERE id < 30 ORDER BY id; ANSWER ============= ID JOB JOB2 -- ----- ---10 Mgr Mgr 20 Sales Sal
Introduction to SQL
36
DB2 9 Cookbook
VALUES Expression (1 of 5)
Syntax
, expression VALUES ( , , expression NULL )
Notes
Define one or more rows of temporary data
Examples
VALUES VALUES VALUES VALUES VALUES 6 (6) 6, 7, 8 (6), (7), (8) (6,66), (7,77), (8,NULL) <= <= <= <= <= 1 1 1 3 3 row, row, row, rows, rows, 1 1 3 1 2 column column columns column columns
Introduction to SQL
37
DB2 9 Cookbook
VALUES Expression (2 of 5)
Define a temporary table #1
WITH temp1 (col1, col2) AS (VALUES ( 0, 'AA') ,( 1, 'BB') ,( 2, NULL) ) SELECT * FROM temp1; ANSWER ========= COL1 COL2 ---- ---0 AA 1 BB 2 -
Introduction to SQL
38
DB2 9 Cookbook
VALUES Expression (3 of 5)
Define a temporary table #3
WITH temp1 (col1, col2) AS (VALUES ( 0, CAST('AA' AS CHAR(1))) ,( 1, CAST('BB' AS CHAR(1))) ,( 2, CAST(NULL AS CHAR(1))) ) SELECT * FROM temp1; ANSWER ========= COL1 COL2 ---- ---0 a 1 b 2 -
Introduction to SQL
39
DB2 9 Cookbook
VALUES Expression (4 of 5)
Derive one temporary table from another
WITH temp1 (col1, col2, (VALUES ( 0, 'AA', ,( 1, 'BB', ,( 2, 'CC', ) ,temp2 (col1b, colx) AS (SELECT col1 ,col1 + col3 FROM temp1 ) SELECT * FROM temp2; col3) AS 0.00) 1.11) 2.22) ANSWER ========== COL1B COLX ----- ---0 0.00 1 2.11 2 4.22
Introduction to SQL
40
DB2 9 Cookbook
VALUES Expression (5 of 5)
Seed a recursive SQL statement
WITH temp1 (col1) AS (VALUES 0 UNION ALL SELECT col1 + 1 FROM temp1 WHERE col1 + 1 < 100 ) SELECT * FROM temp1; ANSWER ====== COL1 ---0 1 2 3 etc
Introduction to SQL
41
DB2 9 Cookbook
CASE Expression (1 of 6)
Syntax
WHEN CASE expression ELSE NULL ELSE result WHEN expression THEN search-condition THEN result NULL result NULL
END
Notes
Lets one have "if then else" logic in a SQL statement First CASE that matches is the one used If none matches, result is null (default) Two syntax styles - see next page
Introduction to SQL
42
DB2 9 Cookbook
CASE Expression (2 of 6)
Use CASE (type 1) to expand a value
SELECT Lastname ,sex AS sx ,CASE sex WHEN 'F' THEN 'FEMALE' WHEN 'M' THEN 'MALE' ELSE NULL END AS sexx FROM employee WHERE lastname LIKE 'J%' ORDER BY 1; ANSWER ==================== LASTNAME SX SEXX ---------- -- -----JEFFERSON M MALE JOHNSON F FEMALE JONES M MALE
Introduction to SQL
43
DB2 9 Cookbook
CASE Expression (3 of 6)
Use CASE to display the higher of two values
SELECT lastname ,midinit AS mi ,sex AS sx ,CASE WHEN midinit > sex THEN midinit ELSE sex END AS mx FROM employee WHERE lastname LIKE 'J%' ORDER BY 1; ANSWER =================== LASTNAME MI SX MX ---------- -- -- -JEFFERSON J M M JOHNSON P F P JONES T M T
FROM WHERE
Introduction to SQL
44
DB2 9 Cookbook
CASE Expression (4 of 6)
Use CASE in a predicate
SELECT lastname ,sex FROM employee WHERE lastname LIKE 'J%' AND CASE sex WHEN 'F' THEN '' WHEN 'M' THEN '' ELSE NULL END IS NOT NULL ORDER BY 1; ANSWER ============== LASTNAME SEX ---------- --JEFFERSON M JOHNSON F JONES M
Introduction to SQL
45
DB2 9 Cookbook
CASE Expression (5 of 6)
UPDATE statement with nested CASE expressions
UPDATE staff SET comm = CASE dept WHEN 15 THEN comm * WHEN 20 THEN comm * WHEN 38 THEN CASE WHEN years < WHEN years >= ELSE NULL END ELSE comm END WHERE comm IS NOT NULL AND dept < 50; 1.1 1.2
Introduction to SQL
46
DB2 9 Cookbook
CASE Expression (6 of 6)
Use CASE to derive a value (correct)
SELECT lastname ,sex ,CASE WHEN sex >= 'M' THEN 'MAL' WHEN sex >= 'F' THEN 'FEM' END AS sxx FROM employee WHERE lastname LIKE 'J%' ORDER BY 1; ANSWER ================= LASTNAME SX SXX ---------- -- --JEFFERSON M MAL JOHNSON F FEM JONES M MAL
Introduction to SQL
47
DB2 9 Cookbook
DML Statements
DML Statements
48
DB2 9 Cookbook
INSERT (1 of 5)
Syntax Diagram
INSERT INTO table-name view-name (full-select) , INCLUDE VALUES WITH ( ( column-name , expression ) full-select common-table-expression data-type ) ( , column-name )
Notes
Used to insert one or more rows into a table Can either insert values directly, or insert from SELECT result See also the MERGE statement
DML Statements
49
DB2 9 Cookbook
INSERT (2 of 5)
Sample Table
CREATE TABLE emp_act (empno CHARACTER ,projno CHARACTER ,actno SMALLINT ,emptime DECIMAL ,emstdate DATE ,emendate DATE); (00006) (00006) (05,02) NOT NULL NOT NULL NOT NULL
Note
If multiple rows are inserted, and any one violates unique index - all are rejected
DML Statements
50
DB2 9 Cookbook
INSERT (3 of 5)
Sample Table
CREATE TABLE emp_act (empno CHARACTER ,projno CHARACTER ,actno SMALLINT ,emptime DECIMAL ,emstdate DATE ,emendate DATE); (00006) (00006) (05,02) NOT NULL NOT NULL NOT NULL
Note
If not all columns are provided (in data), the names have be explicitly listed
DML Statements
51
DB2 9 Cookbook
INSERT (4 of 5)
Sample Table
CREATE TABLE emp_act (empno CHARACTER ,projno CHARACTER ,actno SMALLINT ,emptime DECIMAL ,emstdate DATE ,emendate DATE); (00006) (00006) (05,02) NOT NULL NOT NULL NOT NULL
Note
All matching rows inserted in one unit of work
DML Statements
52
DB2 9 Cookbook
INSERT (5 of 5)
Sample Table (1 of 2)
CREATE TABLE us_customer (cust# INTEGER NOT NULL ,cname CHAR(10) NOT NULL ,country CHAR(03) NOT NULL ,CHECK (country = 'USA') ,PRIMARY KEY (cust#)); CREATE TABLE intl_customer (cust# INTEGER NOT NULL ,cname CHAR(10) NOT NULL ,country CHAR(03) NOT NULL ,CHECK (country <> 'USA') ,PRIMARY KEY (cust#));
Notes
All tables referenced in UNION ALL must have CHECK constraints such that a new row will go to one table, or another, but not more than one, and not none
DML Statements
53
DB2 9 Cookbook
UPDATE
Update Columns - set values
UPDATE SET emp_act emptime ,emendate ,emstdate ,actno ,projno empno = NULL = DEFAULT = CURRENT DATE + 2 DAYS = ACTNO / 2 = 'ABC' = '100000';
WHERE
UPDATE emp_act SET (actno ,emstdate ,projno) = (SELECT MAX(salary) ,CURRENT DATE + 2 DAYS ,MIN(CHAR(id)) FROM staff WHERE id <> 33) WHERE empno LIKE '600%';
DML Statements
54
DB2 9 Cookbook
DELETE
Single-row Delete
ELETE FROM WHERE AND AND emp_act empno projno actno
Mass Delete
DELETE FROM emp_act;
Correlated Delete
DELETE FROM WHERE staff s1 id NOT IN (SELECT MAX(id) FROM staff s2 WHERE s1.dept = s2.dept);
Warning
Delete with care
DML Statements
55
DB2 9 Cookbook
Notes
Embed DML in a SELECT stmt to get results of change Cannot join results, nor use in a nested table expression Useful when the DML creates unknown values (e.g. current timestamp)
Tables
OLD: Returns data values prior to stmt run (for UPDATE and DELETE) NEW: Returns data values after stmt run (for UPDATE and INSERT), and before any AFTER triggers or R.I. rules are applied. FINAL: Returns state final state of data values (for UPDATE and INSERT). If there are any AFTER triggers or R.I. rules that affect the data, an error is returned.
DML Statements
56
DB2 9 Cookbook
SELECT
empno ,projno AS prj ,actno AS act FROM FINAL TABLE (INSERT INTO emp_act VALUES ('200000','ABC',10 ,1,'2003-10-22','2003-11-24') ,('200000','DEF',10 ,1,'2003-10-22','2003-11-24')) ORDER BY 1,2,3;
DML Statements
57
DB2 9 Cookbook
Notes
In the above UPDATE, both old and new values are obtained Old value got by making a temp column, insert old value, then selecting New value got by selecting from NEW table
DML Statements
58
DB2 9 Cookbook
Notes
The above query selects every second row that was deleted The predicate in the query has no impact of the set of rows deleted
DML Statements
59
DB2 9 Cookbook
MERGE (1 of 3)
Syntax
MERGE INTO USING ON table-name or view-name or (full-select) table-name or view-name or (full-select) search-conditions THEN AND search-c UPDATE SET... DELETE SIGNAL... WHEN NOT MATCHED AND ELSE IGNORE search-c THEN INSERT... SIGNAL... corr-name corr-name
WHEN MATCHED
Notes
Used to transfer rows from one table to another. Update or delete (optional) if source row in target. Insert (optional) if source row not in target
DML Statements
60
DB2 9 Cookbook
MERGE (2 of 3)
MERGE INTO old_staff oo USING new_staff nn ON oo.id = nn.id WHEN MATCHED THEN UPDATE SET oo.salary = nn.salary WHEN NOT MATCHED THEN INSERT VALUES (nn.id,'?',nn.salary); OLD_STAFF +-----------------+ |ID|JOB |SALARY | |--|-----|--------| |20|Sales|18171.25| |30|Mgr |17506.75| |40|Sales|18006.00| +-----------------+ NEW_STAFF +----------+ |ID|SALARY | |--|-------| |30|1750.67| |40|1800.60| |50|2065.98| +----------+
AFTER-MERGE ================= ID JOB SALARY -- ----- -------20 Sales 18171.25 30 Mgr 1750.67 40 Sales 1800.60 50 ? 2065.98
Notes
ON statement must identify unique row in target If row matches - update If no match - insert
DML Statements
61
DB2 9 Cookbook
MERGE (3 of 3)
MERGE INTO old_staff oo OLD_STAFF NEW_STAFF USING new_staff nn +-----------------+ +----------+ ON oo.id = nn.id |ID|JOB |SALARY | |ID|SALARY | WHEN MATCHED |--|-----|--------| |--|-------| AND oo.salary < 18000 THEN |20|Sales|18171.25| |30|1750.67| UPDATE |30|Mgr |17506.75| |40|1800.60| SET oo.salary = nn.salary |40|Sales|18006.00| |50|2065.98| WHEN MATCHED +-----------------+ +----------+ AND oo.salary > 18000 THEN DELETE AFTER-MERGE WHEN NOT MATCHED ================= AND nn.id > 10 THEN ID JOB SALARY INSERT -- ----- -------VALUES (nn.id,'?',nn.salary) 20 Sales 18171.25 WHEN NOT MATCHED THEN 30 Mgr 1750.67 SIGNAL SQLSTATE '70001' 50 ? 2065.98 SET MESSAGE_TEXT = 'New ID <= 10';
Notes
If row matches & SALARY < 18K - update If row matches & SALARY > 18K - delete If no match and ID > 10 - insert If no match then - error
DML Statements
62
DB2 9 Cookbook
Compound SQL
Compound SQL
63
DB2 9 Cookbook
label:
Notes
Used to group multiple independent SQL statements Can be embedded in Triggers, Functions, Methods, and Dynamic SQL Need to set statement delimiter to something other than semi-colon
Compound SQL
64
DB2 9 Cookbook
Compound SQL
65
DB2 9 Cookbook
Compound SQL
66
DB2 9 Cookbook
DECLARE Variables
Example
BEGIN ATOMIC DECLARE aaa, bbb, ccc SMALLINT DEFAULT 1; DECLARE ddd CHAR(10) DEFAULT NULL; DECLARE eee INTEGER; SET eee = aaa + 1; UPDATE staff SET comm = aaa ,salary = bbb ,years = eee WHERE id = 10; END
Notes
Used to define control variables that are used in statement Default value can be provided (optional) Standard DB2 data types supported
Compound SQL
67
DB2 9 Cookbook
FOR Statement
Syntax
label: select-stmt FOR DO for-loop-name AS cursor-name END FOR DEFAULT label:
SQL-procedure-stmt ;
Example
BEGIN ATOMIC FOR V1 AS SELECT
dept AS dname ,max(id) AS max_id FROM staff GROUP BY dept HAVING COUNT(*) > 1 ORDER BY dept staff id = id = staff dept = dept = dept <
DO UPDATE SET WHERE UPDATE set WHERE AND END FOR; END
Compound SQL
Notes
Executes a group of statements for each row fetched from a query In this example, does two separate updates for each row fetched
68
DB2 9 Cookbook
Example
BEGIN ATOMIC DECLARE numrows INT DEFAULT 0; UPDATE staff SET salary = 12345 WHERE id < 100; GET DIAGNOSTICS numrows = ROW_COUNT; UPDATE staff SET salary = numrows WHERE id = 10; END
Notes
Returns information about most recently run SQL statement In above example, used to get number of rows updated in UPDATE
Compound SQL
69
DB2 9 Cookbook
IF Statement
Syntax
IF seach condition THEN SQL procedure statement ;
ELSEIF
seach condition
THEN
ELSE
Example
BEGIN ATOMIC DECLARE cur INT; SET cur = MICROSECOND(CURRENT TIMESTAMP); IF cur > 600000 THEN UPDATE staff SET name = CHAR(cur) WHERE id = 10; ELSEIF cur > 300000 THEN UPDATE staff SET name = CHAR(cur) WHERE id = 20; ELSE UPDATE staff SET name = CHAR(cur) WHERE id = 30; END IF; END
Compound SQL
Notes
Used to do basic IF-THENELSE logic In this example, does one of three updates, depending on current timestamp
70
DB2 9 Cookbook
ITERATE Statement
Syntax
ITERATE label
Example
BEGIN ATOMIC DECLARE cntr INT DEFAULT 0; whileloop: WHILE cntr < 60 DO SET cntr = cntr + 10; UPDATE staff SET salary = cntr WHERE id = cntr; ITERATE whileloop; UPDATE staff SET comm = cntr + 1 WHERE id = cntr; END WHILE; END
Notes
Causes the program to return to the beginning of the labeled loop In the above example, the second update never gets performed
Compound SQL
71
DB2 9 Cookbook
LEAVE Statement
Syntax
LEAVE label
Example
BEGIN ATOMIC DECLARE cntr INT DEFAULT 1; whileloop: WHILE 1 <> 2 DO SET cntr = cntr + 1; IF RAND() > 0.99 THEN LEAVE whileloop; END IF; END WHILE; UPDATE staff SET salary = cntr WHERE id = 10; END
Notes
Exits the labeled loop In above example, does random number of iterations, then leaves loop
Compound SQL
72
DB2 9 Cookbook
SIGNAL Statement
Syntax
VALUE SIGNAL SQLSTATE condition-name SET MESSAGE_TEXT = variable-name diagnostic-string sqlstate string
Example
BEGIN ATOMIC DECLARE cntr INT DEFAULT 1; DECLARE emsg CHAR(20); whileloop: WHILE RAND() < .99 DO SET cntr = cntr + 1; END WHILE; SET emsg = '#loops: ' || CHAR(cntr); SIGNAL SQLSTATE '75001' SET MESSAGE_TEXT = emsg; END
Notes
Issues error or warning message
Compound SQL
73
DB2 9 Cookbook
WHILE Statement
Syntax
label: WHILE seach-condition DO SQL-procedure-stmt ;
END WHILE
label:
Example
BEGIN ATOMIC DECLARE c1, C2 INT DEFAULT 1; WHILE c1 < 10 DO WHILE c2 < 20 DO SET c2 = c2 + 1; END WHILE; SET c1 = c1 + 1; END WHILE; UPDATE staff SET salary = c1 ,comm = c2 WHERE id = 10; END
Notes
Repeats one or more statements while condition is true
Compound SQL
74
DB2 9 Cookbook
Column Functions
Column Functions
75
DB2 9 Cookbook
AVG Function (1 of 3)
Syntax
AVG ( ALL DISTINCT expression )
Notes
Gets the average (mean) of a set of non-null rows If now rows match, null returned Beware of loss of precision (see A4 vs. A5 below)
Sample SQL
SELECT AVG(dept) ,AVG(ALL dept) ,AVG(DISTINCT dept) ,AVG(dept/10) ,AVG(dept)/10 staff AVG(dept) > 40; AS AS AS AS AS a1 a2 a3 a4 a5 ANSWER ============== A1 A2 A3 A4 A5 -- -- -- -- -41 41 40 3 4
FROM HAVING
Column Functions
76
DB2 9 Cookbook
AVG Function (2 of 3)
Convert zero to null before doing AVG
SELECT AVG(salary) AS salary ,AVG(comm) AS comm1 ,AVG(CASE comm WHEN 0 THEN NULL ELSE comm END) AS comm2 FROM staff; ANSWER =================== SALARY COMM1 COMM2 ------- ----- ----16675.6 351.9 513.3
FROM WHERE
Column Functions
77
DB2 9 Cookbook
AVG Function (3 of 3)
AVG of Date Column
SELECT FROM AVG(DAYS(birthdate)) ,DATE(AVG(DAYS(birthdate))) employee; ANSWER ================= 1 2 ------ ---------709113 06/27/1942
Column Functions
78
DB2 9 Cookbook
CORRELATION Function
Syntax
CORRELATION CORR ( expression , expression )
Sample SQL
WITH temp1(col1, col2, col3, col4) AS (VALUES (0 ,0 ,0 ,RAND(1)) UNION ALL SELECT col1 + 1 ,col2 - 1 ,RAND() ,RAND() FROM temp1 WHERE col1 <= 1000 ) SELECT DEC(CORRELATION(col1,col1),5,3) ,DEC(CORRELATION(col1,col2),5,3) ,DEC(CORRELATION(col2,col3),5,3) ,DEC(CORRELATION(col3,col4),5,3) FROM temp1; ANSWER =========================== COR11 COR12 COR23 COR34 ------ ------ ------ -----1.000 -1.000 -0.017 -0.005
AS AS AS AS
Column Functions
79
DB2 9 Cookbook
COUNT Function (1 of 2)
Syntax
COUNT ( ALL DISTINCT * expression )
Notes
COUNT(*) counts matching rows COUNT(expression) counts rows with a non-null expression values COUNT(ALL expression) is the same as COUNT(expression) statement COUNT(DISTINCT expression) counts distinct non-null expression values
Sample SQL
SELECT COUNT(*) ,COUNT(INT(comm/10)) ,COUNT(ALL INT(comm/10)) ,COUNT(DISTINCT INT(comm/10)) ,COUNT(DISTINCT INT(comm)) ,COUNT(DISTINCT INT(comm))/10 FROM staff; AS AS AS AS AS AS c1 c2 c3 c4 c5 c6 ANSWER ================= C1 C2 C3 C4 C5 C6 -- -- -- -- -- -35 24 24 19 24 2
Column Functions
80
DB2 9 Cookbook
COUNT Function (2 of 2)
COUNT function with and without GROUP BY
SELECT FROM WHERE UNION SELECT 'NO GP-BY' ,COUNT(*) staff id = -1 AS c1 AS c2 ANSWER ============ C1 C2 -------- -NO GP-BY 0
AS c1 AS c2
Column Functions
81
DB2 9 Cookbook
COUNT_BIG Function
Syntax
COUNT_BIG ( ALL DISTINCT * expression )
Notes
Same COUNT function, but returns BIGINT value
Sample SQL
SELECT COUNT_BIG(*) ,COUNT_BIG(dept) ,COUNT_BIG(DISTINCT dept) ,COUNT_BIG(DISTINCT dept/10) ,COUNT_BIG(DISTINCT dept)/10 staff; AS AS AS AS AS c1 c2 C3 C4 C5 ANSWER =================== C1 C2 C3 C4 C5 --- --- --- --- --35. 35. 8. 7. 0.
FROM
Column Functions
82
DB2 9 Cookbook
COVARIANCE Function
Syntax
COVARIANCE COVAR ( expression , expression )
Sample SQL
WITH temp1(c1, c2, c3, c4) AS (VALUES (0 ,0 ,0 ,RAND(1)) UNION ALL SELECT c1 + 1 ,c2 - 1 ,RAND() ,RAND() FROM temp1 WHERE c1 <= 1000 ) SELECT DEC(COVARIANCE(c1,c1),6,0) ,DEC(COVARIANCE(c1,c2),6,0) ,DEC(COVARIANCE(c2,C3),6,4) ,DEC(COVARIANCE(C3,C4),6,4) FROM temp1; ANSWER =============================== COV11 COV12 COV23 COV34 ------- ------- ------- ------83666. -83666. -1.4689 -0.0004
AS AS AS AS
Column Functions
83
DB2 9 Cookbook
GROUPING Function
GROUPING ( expression )
Notes
Used in CUBE, ROLLUP, and GROUPING SETS statements Identifies what rows came from which grouping set
Sample SQL
SELECT dept ,AVG(salary) AS salary ,GROUPING(dept) AS df FROM staff GROUP BY ROLLUP(dept) ORDER BY dept; ANSWER ================ DEPT SALARY DF ---- -------- -10 20865.86 0 15 15482.33 0 20 16071.52 0 38 15457.11 0 42 14592.26 0 51 17218.16 0 66 17215.24 0 84 16536.75 0 - 16675.64 1
Column Functions
84
DB2 9 Cookbook
MAX Function (1 of 2)
Syntax
MAX ( ALL DISTINCT expression )
Notes
Gets max value from a set of rows The DISTINCT option has no affect If no rows match, null returned
Sample SQL
SELECT MAX(dept) ,MAX(ALL dept) ,MAX(DISTINCT dept) ,MAX(DISTINCT dept/10) staff; ANSWER =============== 1 2 3 4 --- --- --- --84 84 84 8
FROM
Column Functions
85
DB2 9 Cookbook
MAX Function (2 of 2)
MAX Function with Dates
SELECT MAX(hiredate) ,CHAR(MAX(hiredate),USA) ,MAX(CHAR(hiredate,USA)) FROM employee; ANSWER ================================ 1 2 3 ---------- ---------- ---------09/30/1980 09/30/1980 12/15/1976
Column Functions
86
DB2 9 Cookbook
MIN Function
Syntax
MIN ( ALL DISTINCT expression )
Sample SQL
SELECT MIN(dept) ,MIN(ALL dept) ,MIN(DISTINCT dept) ,MIN(DISTINCT dept/10) staff; ANSWER =============== 1 2 3 4 --- --- --- --10 10 10 1
FROM
Column Functions
87
DB2 9 Cookbook
REGRESSION Functions
REGR_AVGX REGR_AVGY REGR_COUNT REGR_INTERCEPT REGR_ICPT REGR_R2 REGR_SLOPE REGR_SXX REGR_SXY REGR_SYY ( expression , expression )
Sample SQL
SELECT REGR_SLOPE(bonus,salary) ,REGR_INTERCEPT(bonus,salary) ,REGR_COUNT(bonus,salary) ,REGR_AVGX(bonus,salary) ,REGR_AVGY(bonus,salary) ,REGR_SXX(bonus,salary) ,REGR_SXY(bonus,salary) ,REGR_SYY(bonus,salary) employee workdept = 'A00'; AS AS AS AS AS AS AS AS r_slope r_icpt r_count r_avgx r_avgy r_sxx r_sxy r_syy
FROM WHERE
Column Functions
88
DB2 9 Cookbook
STDDEV Function
Syntax
STDDEV ( ALL DISTINCT expression )
Sample SQL
ANSWER =============================== A1 S1 S2 S3 S4 -- ------------- ---- ---- ---41 +2.3522355E+1 23.5 23.5 24.1
SELECT AVG(dept) AS a1 ,STDDEV(dept) AS s1 ,DEC(STDDEV(dept),3,1) AS s2 ,DEC(STDDEV(ALL dept),3,1) AS s3 ,DEC(STDDEV(DISTINCT dept),3,1) AS s4 FROM staff;
Column Functions
89
DB2 9 Cookbook
SUM Function
Syntax
SUM ( ALL DISTINCT expression )
Notes
Gets the SUM of a set of numeric values (null if no rows match) If DISTINCT used, duplicate values are ignored Nulls values always ignored Beware loss of precision (see S4 vs. S5 below)
Sample SQL
SELECT SUM(dept) ,SUM(ALL dept) ,SUM(DISTINCT dept) ,SUM(dept/10) ,SUM(dept)/10 staff; AS AS AS AS AS s1 s2 s3 s4 s5 ANSWER ======================== S1 S2 S3 S4 S5 ---- ---- ---- ---- ---1459 1459 326 134 145
FROM
Column Functions
90
DB2 9 Cookbook
VARIANCE Function
Syntax
VARIANCE VAR ( ALL DISTINCT expression )
Sample SQL
ANSWER ============================== A1 V1 V2 V3 V4 -- --------------- --- --- --41 +5.533012244E+2 553 553 582
SELECT AVG(dept) AS a1 ,VARIANCE(dept) AS s1 ,DEC(VARIANCE(dept),4,1) AS s2 ,DEC(VARIANCE(ALL dept),4,1) AS s3 ,DEC(VARIANCE(DISTINCT dept),4,1) AS s4 FROM staff;
Column Functions
91
DB2 9 Cookbook
OLAP Functions
OLAP Functions
92
DB2 9 Cookbook
OLAP Functions
RANK() Returns number of preceding rows + 1 (for given value) If >1 row has same value - all get same rank - next row counts all prior rows - can have gaps in sequence Returns number of distinct preceding values + 1 (for value) If >1 row has same value - all get same rank - next row counts prior values - no gaps in sequence Returns row number Use ORDER BY to get row number within field(s) Uses standard DB2 column functions Gets cumulative totals, and/or running averages
DENSE_RANK()
Example
SELECT s1.job, s1.id, s1.salary ,SUM(salary) OVER(ORDER BY job, id) AS sumsal ,ROW_NUMBER() OVER(ORDER BY job, id) AS r FROM staff s1 ANSWER WHERE s1.name LIKE '%s%' ============================ AND s1.id < 90 JOB ID SALARY SUMSAL R ORDER BY s1.job ----- -- -------- -------- ,s1.id; Clerk 80 13504.60 13504.60 1 Mgr 10 18357.50 31862.10 2 Mgr 50 20659.80 52521.90 3
OLAP Functions
93
DB2 9 Cookbook
asc option
NULLS LAST ASC NULLS FIRST
desc option
NULLS FIRST DESC NULLS LAST
Note
"OVER( ... ORDER BY field(s) )" is mandatory
OLAP Functions
94
DB2 9 Cookbook
NULL
ANSWER =================================== ID YEARS SALARY RANK# DENSE# ROW# -- ----- -------- ----- ------ ---30 5 17506.75 1 1 1 40 6 18006.00 2 2 2 90 6 18001.75 2 2 3 10 7 18357.50 4 3 4 70 7 16502.83 4 3 5 20 8 18171.25 6 4 6 50 10 20659.80 7 5 7
Notes
ORDER BY is mandatory ORDER BY indicates ranking sequence ORDER BY in function has no relationship to ORDER BY in query
OLAP Functions
95
DB2 9 Cookbook
ORDER BY Usage
SELECT job ,years ,id ,name ,SMALLINT(RANK() OVER(ORDER BY job ASC)) AS asc1 ,SMALLINT(RANK() OVER(ORDER BY job ASC ,years ASC)) AS asc2 ,SMALLINT(RANK() OVER(ORDER BY job ASC ,years ASC ,id ASC)) AS asc3 ,SMALLINT(RANK() OVER(ORDER BY job DESC)) AS dsc1 ,SMALLINT(RANK() OVER(ORDER BY job DESC ,years DESC)) AS dsc2 ,SMALLINT(RANK() OVER(ORDER BY job DESC ,years DESC ,id DESC)) AS dsc3 ,SMALLINT(RANK() OVER(ORDER BY job ASC ,years DESC ,id ASC)) AS mix1 ,SMALLINT(RANK() OVER(ORDER BY job DESC ,years ASC ,id DESC)) AS mix2 staff id < 150 years IN (6,7) job > 'L' BY job ANSWER ,years =========================================================== ,id; JOB Y ID NAME ASC1 ASC2 ASC3 DSC1 DSC2 DSC3 MIX1 MIX2 ----- - --- ------- ---- ---- ---- ---- ---- ---- ---- ---Mgr 6 140 Fraye 1 1 1 4 6 6 3 4 Mgr 7 10 Sanders 1 2 2 4 4 5 1 6 Mgr 7 100 Plotz 1 2 3 4 4 4 2 5 Sales 6 40 O'Brien 4 4 4 1 2 3 5 2 Sales 6 90 Koonitz 4 4 5 1 2 2 6 1 Sales 7 70 Rothman 4 6 6 1 1 1 4 3
96
OLAP Functions
DB2 9 Cookbook
ANSWER ================================== ID YR SALARY A AF AL D DF DL -- -- -------- -- -- -- -- -- -30 5 17506.75 1 2 1 6 6 5 90 6 18001.75 2 3 2 5 5 4 40 6 18006.00 2 3 2 5 5 4 70 7 16502.83 3 4 3 4 4 3 10 7 18357.50 3 4 3 4 4 3 20 8 18171.25 4 5 4 3 3 2 50 10 20659.80 5 6 5 2 2 1 80 - 13504.60 6 1 6 1 1 6 60 - 16808.30 6 1 6 1 1 6
Notes
Can define if NULLS rank high or low By default - rank high for an ascending field, low for a descending field
OLAP Functions
97
DB2 9 Cookbook
Partition Usage
SELECT id ,years AS YR ,salary ,RANK() OVER(PARTITION BY years ORDER BY salary) AS r1 FROM staff WHERE id < 80 AND years IS NOT NULL ORDER BY years ,salary; ANSWER ================= ID YR SALARY R1 -- -- -------- -30 5 17506.75 1 40 6 18006.00 1 70 7 16502.83 1 10 7 18357.50 2 20 8 18171.25 1 50 0 20659.80 1
Notes
Rank values by subset of matching rows In above example, rows ranked by salary within year
OLAP Functions
98
DB2 9 Cookbook
Subsequent Processing
SELECT XXX.* ,RANK()OVER(ORDER BY id) AS r2 FROM (SELECT id ,name ,RANK() OVER(ORDER BY id) AS r1 FROM staff WHERE id < 100 AND years IS NOT NULL )AS xxx WHERE id > 30 ORDER BY id; ANSWER ================ ID NAME R1 R2 -- ------- -- -40 O'Brien 4 1 50 Hanes 5 2 70 Rothman 6 3 90 Koonitz 7 4
Notes
RANK function gets rank, as of when the function is applied Subsequent processing can change the rank (in final output) In above example, rows are removed after getting R1 values
OLAP Functions
99
DB2 9 Cookbook
Notes
Query orders output by rank of salary within department
OLAP Functions
100
DB2 9 Cookbook
Notes
In above example, rank is worked out, then subsequently queried
OLAP Functions
101
DB2 9 Cookbook
Notes
Numbers rows returned Output type is BIGINT
OLAP Functions
102
DB2 9 Cookbook
ORDER BY Usage
SELECT id ,name ,ROW_NUMBER() ,ROW_NUMBER() ,ROW_NUMBER() FROM staff WHERE id < 50 AND years IS NOT ORDER BY id; ANSWER ==================== ID NAME R1 R2 R3 -- -------- -- -- -10 Sanders 1 1 4 20 Pernal 2 2 3 30 Marenghi 3 3 1 40 O'Brien 4 4 2
NULL
Notes
ORDER BY in function is not related to ORDER BY in output ORDER BY is not required, but is wise to have If not supplied, result can be meaningless
OLAP Functions
103
DB2 9 Cookbook
PARTITION Usage
SELECT job ,years ,id ,name ,ROW_NUMBER() OVER(PARTITION BY job ORDER BY years) AS row# ,RANK() OVER(PARTITION BY job ORDER BY years) AS rn1# ,DENSE_RANK() OVER(PARTITION BY job ORDER BY years) AS rn2# staff id < 150 years IN (6,7) ANSWER job > 'L' ====================================== BY job JOB YEARS ID NAME ROW# RN1# RN2# ,years; ----- ----- --- ------- ---- ---- ---Mgr 6 140 Fraye 1 1 1 Mgr 7 10 Sanders 2 2 2 Mgr 7 100 Plotz 3 2 2 Sales 6 40 O'Brien 1 1 1 Sales 6 90 Koonitz 2 1 1 Sales 7 70 Rothman 3 3 2
Notes
Assign row numbers, by subset of matching rows In above example, rows numbers by year within job
OLAP Functions 104
DB2 9 Cookbook
OLAP Functions
105
DB2 9 Cookbook
FROM WHERE AND )AS xxx WHERE r BETWEEN 3 AND 6 ORDER BY id;
OLAP Functions
106
DB2 9 Cookbook
Notes
ROW_NUMBER is more useful that FIRST FETCH notation Beware selecting "n" rows if "n+1" row has the same value as "n"
OLAP Functions
107
DB2 9 Cookbook
partitioning expression
) )
DB2 9 Cookbook
Example
SELECT id ,name ,salary ,SUM(salary) ,AVG(salary) ,MIN(salary) ,MAX(salary) ,COUNT(*) FROM staff WHERE id < 60 ORDER BY id;
AS AS AS AS AS
OLAP Functions
109
DB2 9 Cookbook
Example
SELECT id ,name ,salary ,SUM(salary) ,AVG(salary) ,MIN(salary) ,MAX(salary) ,COUNT(*) FROM staff WHERE id < 60 ORDER BY id;
AS AS AS AS AS
ANSWER ===================================================================== ID NAME SALARY SUM_SAL AVG_SAL MIN_SAL MAX_SAL #ROWS -- -------- -------- -------- -------- -------- -------- ----10 Sanders 18357.50 92701.30 18540.26 17506.75 20659.80 5 20 Pernal 18171.25 92701.30 18540.26 17506.75 20659.80 5 30 Marenghi 17506.75 92701.30 18540.26 17506.75 20659.80 5 40 O'Brien 18006.00 92701.30 18540.26 17506.75 20659.80 5 50 Hanes 20659.80 92701.30 18540.26 17506.75 20659.80 5
Notes
Above example selects detailed data, plus summary data - all on one line.
OLAP Functions
110
DB2 9 Cookbook
OVER() Usage
SELECT id ,name ,salary ,SUM(salary) ,SUM(salary) ,SUM(salary) ,SUM(salary)
FROM staff WHERE id < 60 ORDER BY id; ANSWER ============================================================== ID NAME SALARY SUM1 SUM2 SUM3 SUM4 -- -------- -------- -------- -------- -------- -------10 Sanders 18357.50 92701.30 92701.30 92701.30 92701.30 20 Pernal 18171.25 92701.30 92701.30 92701.30 92701.30 30 Marenghi 17506.75 92701.30 92701.30 92701.30 92701.30 40 O'Brien 18006.00 92701.30 92701.30 92701.30 92701.30 50 Hanes 20659.80 92701.30 92701.30 92701.30 92701.30
Notes
OVER() is same as having ORDER BY of field that is same for all matching rows All of the above aggregations are equivalent
OLAP Functions
111
DB2 9 Cookbook
ORDER BY Usage
SELECT dept ,name ,salary ,SUM(salary) ,SUM(salary) ,SUM(salary) ,SUM(salary) ,COUNT(*) ,COUNT(*) FROM staff WHERE id < 60 ORDER BY dept ,name;
BY BY BY BY BY BY
dept) AS SUM1 dept DESC) AS SUM2 dept, name) AS SUM3 dept DESC, name DESC) AS SUM4 dept) AS ROW1 dept, name) AS ROW2
ANSWER ==================================================================== DEPT NAME SALARY SUM1 SUM2 SUM3 SUM4 ROW1 ROW2 ---- -------- -------- -------- -------- -------- -------- ---- ---15 Hanes 20659.80 20659.80 92701.30 20659.80 92701.30 1 1 20 Pernal 18171.25 57188.55 72041.50 38831.05 72041.50 3 2 20 Sanders 18357.50 57188.55 72041.50 57188.55 53870.25 3 3 38 Marenghi 17506.75 92701.30 35512.75 74695.30 35512.75 5 4 38 O'Brien 18006.00 92701.30 35512.75 92701.30 18006.00 5 5
Notes
Provides set of values to aggregate on. Each distinct value gets a new result Gives a direction for aggregation (i.e. ASC or DESC)
OLAP Functions
112
DB2 9 Cookbook
ROWS Usage
SELECT dept ,name ,years ,SMALLINT(SUM(years) OVER(ORDER BY dept)) AS d ,SMALLINT(SUM(years) OVER(ORDER BY dept, name)) AS dn ,SMALLINT(SUM(years) OVER(ORDER BY dept, name ROWS UNBOUNDED PRECEDING))AS dnu ,SMALLINT(SUM(years) OVER(ORDER BY dept, name ROWS 3 PRECEDING)) AS dn3 FROM staff WHERE id < 100 ANSWER AND years IS NOT NULL ================================= ORDER BY dept DEPT NAME YEARS D DN DNU DN3 ,name; ---- -------- ----- -- -- --- --15 Hanes 10 17 10 10 10 15 Rothman 7 17 17 17 17 20 Pernal 8 32 25 25 25 20 Sanders 7 32 32 32 32 38 Marenghi 5 43 37 37 27 38 O'Brien 6 43 43 43 26 42 Koonitz 6 49 49 49 24
Notes
ROWS phrase limits aggregation to subset of matching rows Options are: UNBOUNDED PRECEDING Integer PRECEDING CURRENT ROW BETWEEN ....
OLAP Functions
113
DB2 9 Cookbook
Notes
BETWEEN answer depends on ORDER BY sequence
OLAP Functions
114
DB2 9 Cookbook
Notes
Preceding row is always on LEFT of current row Following row is always on RIGHT of current row
SELECT id ,NAME ,SMALLINT(SUM(id) OVER(ORDER BY id ASC ROWS BETWEEN 1 PRECEDING ,SMALLINT(SUM(id) OVER(ORDER BY id ASC ROWS BETWEEN CURRENT ROW ,SMALLINT(SUM(id) OVER(ORDER BY id DESC ROWS BETWEEN 1 PRECEDING ,SMALLINT(SUM(id) OVER(ORDER BY id DESC ROWS BETWEEN CURRENT ROW FROM staff WHERE id < 50 AND years IS NOT NULL ORDER BY id;
AND CURRENT ROW)) AS apc AND 1 FOLLOWING)) AS acf AND CURRENT ROW)) AS dpc AND 1 FOLLOWING)) AS dcf
OLAP Functions
115
DB2 9 Cookbook
PARTITION Usage
SELECT dept ,name ,years AS yr ,SMALLINT(SUM(years) OVER(ORDER BY dept)) AS x ,SMALLINT(SUM(years) OVER(ORDER BY dept ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING)) AS y ,SMALLINT(SUM(years) OVER(PARTITION BY dept)) AS p ,SMALLINT(SUM(years) OVER(PARTITION BY dept ORDER BY dept)) AS po ,SMALLINT(SUM(years) OVER(PARTITION BY dept ORDER BY dept ROWS 1 PRECEDING)) AS p1 FROM staff WHERE id BETWEEN 40 AND 120 ANSWER AND years IS NOT NULL ================================ ORDER BY dept DEPT NAME YR X Y P PO P1 ,name; ----- ------- -- -- -- -- -- -15 Hanes 10 22 15 22 22 10 15 Ngan 5 22 22 22 22 15 15 Rothman 7 22 18 22 22 12 38 O'Brien 6 28 19 6 6 6 42 Koonitz 6 41 19 13 13 6 42 Plotz 7 41 13 13 13 13
Notes
PARTITION phrase limits aggregation to set of common values
OLAP Functions
116
DB2 9 Cookbook
Scalar Functions
Scalar Functions
117
DB2 9 Cookbook
DB2 9 Cookbook
BIGINT Function
WITH temp (big) AS (VALUES BIGINT(1) UNION ALL SELECT big * 256 FROM temp WHERE big < 1E16 ) SELECT big FROM temp; ANSWER ==================== BIG -------------------1 256 65536 16777216 4294967296 1099511627776 281474976710656 72057594037927936
Notes
Converts number to big-integer
Scalar Functions
119
DB2 9 Cookbook
CHAR Function (1 of 3)
CHAR ( character value date-time value integer value double value decimal value , dec.pt , length , format )
Notes
Converts numbers to character (decimal works differently from others) Converts character-expression to character (fixed length) Converts date-time values to character
Example
SELECT name ,CHAR(name,3) ,comm ,CHAR(comm) ,CHAR(comm,'@') FROM staff WHERE id BETWEEN 80 AND 100 ORDER BY id; ANSWER ===================================== NAME 2 COMM 4 5 ------- --- ------- -------- -------James Jam 128.20 00128.20 00128@20 Koonitz Koo 1386.70 01386.70 01386@70 Plotz Plo - -
Scalar Functions
120
DB2 9 Cookbook
CHAR Function (2 of 3)
Positive Numbers
ANSWER ========================================== INT CHAR_INT CHAR_FLT CHAR_DEC -------- -------- ----------- -----------3 3 3.0E0 00000000003. 9 9 9.0E0 00000000009. 81 81 8.1E1 00000000081. 6561 6561 6.561E3 00000006561. 43046721 43046721 4.3046721E7 00043046721. AS AS AS AS int char_int char_flt char_dec
WITH temp1 (n) AS (VALUES (3) UNION ALL SELECT n * n FROM temp1 WHERE n < 9000 ) SELECT n ,CHAR(INT(n)) ,CHAR(FLOAT(n)) ,CHAR(DEC(n)) FROM temp1;
Note
Negative values get preceding "-" (digits not aligned)
Scalar Functions
121
DB2 9 Cookbook
CHAR Function (3 of 3)
Date Input
SELECT CHAR(hiredate,ISO) ,CHAR(hiredate,USA) ,CHAR(hiredate,EUR) FROM employee WHERE lastname < 'C' ORDER BY 2; ANSWER ================================ 1 2 3 ---------- ---------- ---------1972-02-12 02/12/1972 12.02.1972 1966-03-03 03/03/1966 03.03.1966
Scalar Functions
122
DB2 9 Cookbook
COALESCE Function
SELECT id ,comm ,COALESCE(comm,0) FROM staff WHERE id < 30 ORDER BY id; ANSWER ================== ID COMM 3 -- ------ -----10 0.00 20 612.45 612.45
Notes
Returns first non-null value in list Same as VALUE function Useful in outer joins
Scalar Functions
123
DB2 9 Cookbook
CONCAT Function (1 of 2)
SELECT 'A' || 'B' ,'A' CONCAT 'B' ,CONCAT('A','B') ,'A' || 'B' || 'C' ,CONCAT(CONCAT('A','B'),'C') staff id = 10; ANSWER =================== 1 2 3 4 5 --- --- --- --- --AB AB AB ABC ABC
FROM WHERE
Notes
Joins two strings together
Syntax Styles
"AB" || "CD" "AB" CONCAT "CD" CONCAT("AB","CD")
Scalar Functions
124
DB2 9 Cookbook
CONCAT Function (2 of 2)
CONCAT used with ORDER BY - wrong output sequence
WITH temp1 (col1, col2) AS (VALUES ('A' , 'YYY') ,('AE', 'OOO') ,('AE', 'YYY') ) SELECT col1 ,col2 ,col1 CONCAT col2 AS col3 FROM temp1 ORDER BY col3; ANSWER =============== COL1 COL2 COL3 ---- ---- ----AE OOO AEOOO AE YYY AEYYY A YYY AYYY
Scalar Functions
125
DB2 9 Cookbook
DAYOFWEEK_ISO Function
WITH temp1 (n) AS (VALUES (0) UNION ALL SELECT n+1 FROM temp1 WHERE n < 9), temp2 (dt1) AS (VALUES(DATE('1999-12-26')) ,(DATE('2000-12-24'))), temp3 (dt2) AS (SELECT dt1 + n DAYS FROM temp1 ,temp2) SELECT CHAR(dt2,ISO) ,SUBSTR(DAYNAME(dt2),1,3) ,WEEK(dt2) ,DAYOFWEEK(dt2) ,WEEK_ISO(dt2) ,DAYOFWEEK_ISO(dt2) FROM temp3 ORDER BY 1; ANSWER ======================== DATE DAY W D WI I ---------- --- -- - -- 1999-12-28 Tue 53 3 52 2 1999-12-29 Wed 53 4 52 3 1999-12-30 Thu 53 5 52 4 1999-12-31 Fri 53 6 52 5 2000-01-01 Sat 1 7 52 6 2000-01-02 Sun 2 1 52 7 2000-01-03 Mon 2 2 1 1 2000-01-04 Tue 2 3 1 2 2000-12-24 Sun 53 1 51 7 2000-12-25 Mon 53 2 52 1 2000-12-26 Tue 53 3 52 2 2000-12-27 Wed 53 4 52 3 2000-12-28 Thu 53 5 52 4 2000-12-29 Fri 53 6 52 5 2000-12-30 Sat 53 7 52 6 2000-12-31 Sun 54 1 52 7 2001-01-01 Mon 1 2 1 1 2001-01-02 Tue 1 3 1 2
AS AS AS AS AS AS
date day w d wi i
Warning
Weeks are not fixed items - use with care
Scalar Functions
126
DB2 9 Cookbook
DECIMAL Function
DECIMAL DEC ( char , precision , scale , dec ( number , precision , scale ) )
Examples
WITH temp1(n1,n2,c1,c2) AS (VALUES (123 ,1E2 ,'123.4' ,'567$8')) SELECT DEC(n1,3) AS dec1 ,DEC(n2,4,1) AS dec2 ,DEC(c1,4,1) AS dec3 ,DEC(c2,4,1,'$') AS dec4 FROM temp1; ANSWER ========================== DEC1 DEC2 DEC3 DEC4 ----- ------ ------ -----123. 100.0 123.4 567.8
Scalar Functions
127
DB2 9 Cookbook
GENERATE_UNIQUE Function
SELECT id ,GENERATE_UNIQUE() AS unique_val#1 ,DEC(HEX(GENERATE_UNIQUE()),26) AS unique_val#2 FROM staff WHERE id < 50 ORDER BY id; ANSWER ================= ID UNIQUE_VAL#1 -- -------------10 20 30 40
Scalar Functions
128
DB2 9 Cookbook
LOCATE Function
LOCATE ( find-string , look-in-string , start-pos. )
Examples
SELECT c1 ,LOCATE('D', c1) ,LOCATE('D', c1,2) ,LOCATE('EF',c1) ,LOCATE('A', c1,2) FROM scalar; ANSWER ========================== C1 2 3 4 5 ------ --- --- --- --ABCDEF 4 4 5 0 ABCD 4 4 0 0 AB 0 0 0 0
Notes
Returns location of one string within another Returns zero if no value found
Scalar Functions
129
DB2 9 Cookbook
RAISE_ERROR Function
RAISE_ERROR ( sqlstate ,error-message )
Example
SELECT S1 ,CASE WHEN s1 < 1 THEN s1 ELSE RAISE_ERROR('80001',c1) END AS s2 FROM scalar; ANSWER ============== S1 S2 ------ ------2 -2 0 0 SQLSTATE=80001
Notes
Causes the SQL to stop and return a user-defined error See SQL Reference for (many) usage restrictions
Scalar Functions
130
DB2 9 Cookbook
RAND Function (1 of 4)
WITH temp (num, ran) AS (VALUES (INT(1) ,RAND(2)) UNION ALL SELECT num + 1 ,RAND() FROM temp WHERE num < 100000 ) SELECT COUNT(*) ,COUNT(DISTINCT ran) ,DEC(AVG(ran),7,6) ,DEC(STDDEV(ran),7,6) ,DEC(MIN(ran),7,6) ,DEC(MAX(ran),7,6) ,DEC(MAX(ran),7,6) DEC(MIN(ran),7,6) ,DEC(VAR(ran),7,6) FROM temp;
AS AS AS AS AS AS
ANSWER ============= 100000 31242 0.499838 0.288706 0.000000 1.000000 1.000000 0.083351
AS range AS variance
Warnings
Beware using RAND in predicates Returns different results in different flavors of DB2
Scalar Functions
131
DB2 9 Cookbook
RAND Function (2 of 4)
Make reproducible random numbers (use seed)
SELECT deptno AS dno ,RAND(0) AS ran FROM department WHERE deptno < 'E' ORDER BY 1; ANSWER =========================== DNO RAN --- ---------------------A00 +1.15970336008789E-003 B01 +2.35572374645222E-001 C01 +6.48152104251228E-001 D01 +7.43736075930052E-002 D11 +2.70241401409955E-001 D21 +3.60026856288339E-001
Notes
Returns floating-point pseudo-random values in range zero to one Seed can be provided to get reproducible values Maximum of 32K distinct values generated
Scalar Functions
132
DB2 9 Cookbook
RAND Function (3 of 4)
Use RAND to make sample data
WITH temp1 (col1, col2, col3) AS (VALUES (0 ,SMALLINT(RAND(2)*35)*10 ,DECIMAL(RAND()*10000,7,2)) UNION ALL SELECT col1 + 1 ,SMALLINT(RAND()*35)*10 ,DECIMAL(RAND()*10000,7,2) FROM temp1 WHERE col1 + 1 < 10 ) SELECT * FROM temp1; ANSWER =================== COL1 COL2 COL3 ---- ---- ------0 0 9342.32 1 250 8916.28 2 310 5430.76 3 150 5996.88 4 110 8066.34 5 50 5589.77 6 130 8602.86 7 340 184.94 8 310 5441.14 9 70 9267.55
Scalar Functions
133
DB2 9 Cookbook
RAND Function (4 of 4)
Randomly select 10% of matching rows
SELECT id ,name FROM staff WHERE RAND() < 0.1 ORDER BY id; ANSWER ============ ID NAME --- -------140 Fraye 190 Sneider 290 Quill
Warning
Beware using RAND in predicates
Scalar Functions
134
DB2 9 Cookbook
SUBSTR Function (1 of 2)
SUBSTR ( string , start , length )
Notes
Returns part of a string If no length, output is from START to end of string If LENGTH longer than string, error results
Scalar Functions
135
DB2 9 Cookbook
SUBSTR Function (2 of 2)
Avoid LENGTH error using CASE
WITH temp1 (len, dat1) AS ANSWER (VALUES ( 6,'123456789') ========================= ,( 4,'12345' ) LEN DAT1 LDAT SUBDAT ,( 16,'123' ) --- --------- ---- -----) 6 123456789 9 123456 SELECT len 4 12345 5 1234 ,dat1 16 123 3 123 ,LENGTH(dat1) AS ldat ,SUBSTR(dat1,1,CASE WHEN len < LENGTH(dat1) THEN LEN ELSE LENGTH(dat1) END ) AS subdat FROM temp1;
Scalar Functions
136
DB2 9 Cookbook
TRANSLATE Function
TRANSLATE ( string , to , from , substitute )
Examples
ANS. ==== abcd ABCD bcd abcd Abcd A cd A cd Azcd NOTES ================= No change Make upper case 'a'=>' ' 'A'=>'A' 'a'=>'A' 'a'=>'A','b'=>' ' 'a'=>'A','b'=>' ' 'a'=>'A','b'=>'z'
SELECT 'abcd' ,TRANSLATE('abcd') ,TRANSLATE('abcd','','a') ,TRANSLATE('abcd','A','A') ,TRANSLATE('abcd','A','a') ,TRANSLATE('abcd','A','ab') ,TRANSLATE('abcd','A','ab',' ') ,TRANSLATE('abcd','A','ab','z') FROM staff WHERE id = 10; SELECT dt1 ,YEAR(dt1) AS yr ,WEEK(dt1) AS wk FROM scalar;
ANSWER ====================== DT1 YR WK ---------- ---- ---04/22/1996 1996 17 08/15/1996 1996 33 01/01/0001 1 1
Scalar Functions
137
DB2 9 Cookbook
User-Defined Functions
User-Defined Functions
138
DB2 9 Cookbook
Example
CREATE FUNCTION digi_int (SMALLINT) RETURNS CHAR(5) SOURCE SYSIBM.DIGITS(SMALLINT);
Notes
Used to redefine an existing DB2 function for specific data types In above example, based on DIGITS function, but only works on SMALLINT fields
User-Defined Functions
139
DB2 9 Cookbook
Query Fails
SELECT id ,balance * 10 FROM customers ORDER BY id; ANSWER ======= <error>
Notes
Sourced functions are often need to process Distinct Data Types
User-Defined Functions
140
DB2 9 Cookbook
Example
CREATE FUNCTION returns_zero() RETURNS SMALLINT RETURN 0; SELECT FROM WHERE id ,returns_zero() staff id = 10; AS id AS ZZ ANSWER ====== ID ZZ -- -10 0
User-Defined Functions
141
DB2 9 Cookbook
READS SQL DATA CONTAINS SQL PREDICATES RETURN value NULL , WITH (
STATIC DISPATCH
predicate-list
full-select common-table-expression
User-Defined Functions
142
DB2 9 Cookbook
User-Defined Functions
143
DB2 9 Cookbook
Notes
If two functions exist with the same name, DB2 chooses which one to use based on the input data types
User-Defined Functions
144
DB2 9 Cookbook
Notes
Function is non-deterministic because the output can be not be determined - based on the input value
User-Defined Functions
145
DB2 9 Cookbook
Notes
Function invokes complex query that returns a single value Gets MAX salary in same DEPT, or in same YEAR whichever higher
SELECT
id AS id ,salary AS sal1 ,max_sal(id) AS sal2 FROM staff WHERE id < 40 ORDER BY id;
ANSWER ==================== ID SAL1 SAL2 -- -------- -------10 18357.50 22959.20 20 18171.25 18357.50 30 17506.75 19260.25
User-Defined Functions
146
DB2 9 Cookbook
CREATE FUNCTION reverse(instr VARCHAR(50)) RETURNS VARCHAR(50) BEGIN ATOMIC DECLARE outstr VARCHAR(50) DEFAULT ''; DECLARE curbyte SMALLINT DEFAULT 0; SET curbyte = LENGTH(RTRIM(instr)); WHILE curbyte >= 1 DO SET outstr = outstr || SUBSTR(instr,curbyte,1); SET curbyte = curbyte - 1; END WHILE; RETURN outstr; END! ANSWER SELECT id AS id ==================== ,name AS name1 ID NAME1 NAME2 ,reverse(name) AS name2 -- -------- ------FROM staff 10 Sanders srednaS WHERE id < 40 20 Pernal lanreP ORDER BY id! 30 Marenghi ihgneraM
Notes
Above function uses compound SQL to reverse contents of string Because has compound SQL, cannot be defined using semi-colon terminator
User-Defined Functions
147
DB2 9 Cookbook
ANSWER ================= ID NAME1 NAME2 -- -------- ----10 Sanders 7 20 Pernal -1 30 Marenghi 8 40 O'Brien 7 <error>
Notes
If length of input string is less than 6, error is flagged
User-Defined Functions
148
DB2 9 Cookbook
Notes
Returns table (set of rows and columns) Most of the syntax is the same as for SCALAR functions
Example
CREATE FUNCTION get_staff() RETURNS TABLE (ID SMALLINT ,name VARCHAR(9) ,YR SMALLINT) RETURN SELECT id ,name ,years FROM staff; SELECT FROM WHERE ORDER BY * TABLE(get_staff()) AS s id < 40 id;
User-Defined Functions
149
DB2 9 Cookbook
Example of Use
SELECT FROM WHERE ORDER BY * TABLE(staff_list(66,1)) AS ttt id < 111 id;
ANSWER ============================ ID SALARY MAX_SAL ID_MAX --- -------- -------- -----70 16502.83 22959.20 160 80 13504.60 22959.20 160 90 18001.75 22959.20 160 100 18352.80 22959.20 160 110 12508.20 22959.20 160
150
DB2 9 Cookbook
151
DB2 9 Cookbook
ORDER BY
Syntax
, ASC ORDER BY column name column# expression DESC
Simple ORDER BY
SELECT col1 ,col2 FROM seq_data ORDER BY col1 ASC ,col2; ANSWER ========= COL1 COL2 ---- ---ab xy ac XY Ab 12 AB xy AB XY
152
DB2 9 Cookbook
ORDER BY Examples (1 of 2)
Case Insensitive
SELECT col1 ,col2 FROM seq_data ORDER BY TRANSLATE(col1) ASC ,TRANSLATE(col2) ASC ANSWER ========= COL1 COL2 ---- ---Ab 12 ab xy AB XY AB xy ac XY
On non-displayed Column
SELECT col2 FROM seq_data ORDER BY col1 ,col2; ANSWER ====== COL2 ---xy XY 12 xy XY
153
DB2 9 Cookbook
ORDER BY Examples (2 of 2)
On second byte of first column
SELECT col1 ,col2 FROM seq_data ORDER BY SUBSTR(col1,2) DESC ,col2 ,1; ANSWER ========= COL1 COL2 ---- ---ac XY AB xy AB XY Ab 12 ab xy
In bit-data sequence
SELECT col1 ,HEX(col1) AS hex1 ,col2 ,HEX(col2) AS hex2 FROM seq_data ORDER BY HEX(col1) ,HEX(col2) ANSWER =================== COL1 HEX1 COL2 HEX2 ---- ---- ---- ---AB 4142 XY 5859 AB 4142 xy 7879 Ab 4162 12 3132 ab 6162 xy 7879 ac 6163 XY 5859
154
DB2 9 Cookbook
GROUP BY
, GROUP BY expression , GROUPING SETS grand-total , ROLLUP ( expression ( , CUBE ( expression ( ( HAVING ) , expression ) ) , expression ) ) ( expression ROLLUP stmt (see below) CUBE stmt (see below) ( ) )
search-condition(s)
Notes
GROUP BY rolls up multiple rows into one
Order By, Group By, and Having 155
DB2 9 Cookbook
Notes
Observe, first three fields are non-unique
156
DB2 9 Cookbook
Notes
Get one row per set of values in GROUP BY field list WHERE predicates applied before GROUP BY HAVING predicates applied during GROUP BY
157
DB2 9 Cookbook
ANSWER ================== WPART SALARY #ROWS ----- ------ ----1 353820 13 0 128500 3
158
DB2 9 Cookbook
GROUPING SETS
Parenthesis Usage
GROUP BY GROUPING SETS ((a,b,c)) is equivalent to GROUP BY A ,B ,C GROUP UNION GROUP UNION GROUP BY A ALL BY B ALL BY C
is equivalent to
is equivalent to
Notes
GROUP BY GROUPING SETS is used to get multiple groupings in one stmt Parenthesis rules: - Nested list of columns works as simple GROUP BY - Non-nested list of columns works separate GROUP BY stmts, separated by a UNION
159
DB2 9 Cookbook
is equivalent to
is equivalent to
Notes
Multiple GROUPING SETS in the same GROUP BY are combined as if they were single fields in a GROUP BY list
160
DB2 9 Cookbook
ROLLUP
GROUP BY GROUPING SETS ((a,b,c) ,(a,b) ,(a) ,()) is equivalent to GROUP BY A ,B ,C UNION ALL GROUP BY A ,B UNION ALL GROUP BY A UNION ALL grand-totl
is equivalent to
ROLLUP(a,b,c)
Notes
ROLLUP of "n" fields Does GROUP BY on all fields listed, plus GROUP BY subsets of the fields (from left), plus Grand total of all fields
Example
SELECT dept ,SUM(salary) AS salary ,SMALLINT(COUNT(*)) AS #rows ,GROUPING(dept) AS FD FROM employee_view GROUP BY ROLLUP(dept) ORDER BY dept; ANSWER ==================== DEPT SALARY #ROWS FD ---- ------ ----- -A00 128500 3 0 B01 41250 1 0 C01 90470 3 0 D11 222100 9 0 482320 16 1
161
DB2 9 Cookbook
CUBE
GROUP BY GROUPING SETS ((a,b,c) ,(a,b) ,(a,c) ,(b,c) ,(a) ,(b) ,(c) ,()) is equivalent to GROUP BY A ,B ,C UNION ALL GROUP BY A ,B UNION ALL GROUP BY A ,C UNION ALL GROUP BY B ,C UNION ALL GROUP BY A UNION ALL GROUP BY B UNION ALL GROUP BY C UNION ALL grand-totl
is equivalent to
CUBE(a,b,c)
Notes
CUBE of "n" fields Does GROUP BY on all fields listed, plus GROUP BY on <all> subsets of the fields, plus Grand total of all fields
162
DB2 9 Cookbook
163
DB2 9 Cookbook
WHERE OR OR OR OR OR ORDER
Predicates used - Explanation ========================================== (G1,GD,GS) = (0,0,0) <== D1, DEPT, SEX (G1,GD,GS) = (0,0,1) <== D1, DEPT (G1,GD,GS) = (0,1,0) <== D1, SEX (G1,GD,GS) = (0,1,1) <== D1, (G1,GD,GS) = (1,1,0) <== SEX, (G1,GD,GS) = (1,1,1) <== grand total
164
DB2 9 Cookbook
Joins
Joins
165
DB2 9 Cookbook
CREATE VIEW staff_v2 AS SELECT id, job FROM staff WHERE id BETWEEN 20 AND 50 UNION ALL SELECT id, 'Clerk' AS job FROM staff WHERE id = 30;
Notes
Both views have ID values that do not exist in the other view In the V2 view, there are two rows for ID = 30
Joins
166
DB2 9 Cookbook
Notes
Best used for inner joins Sequence in which tables listed does not affect join logic
Example
SELECT v1.id ,v1.name ,v2.job FROM staff_v1 v1 ,staff_v2 v2 WHERE v1.id = v2.id ORDER BY v1.id ,v2.job; JOIN ANSWER ================= ID NAME JOB -- -------- ----20 Pernal Sales 30 Marenghi Clerk 30 Marenghi Mgr STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Joins
167
DB2 9 Cookbook
Notes
Suitable for both inner and outer joins Sequence in which tables listed affects join logic
Example
SELECT v1.id ,v1.name ,v2.job FROM staff_v1 v1 INNER JOIN staff_v2 v2 ON v1.id = v2.id ORDER BY v1.id ,v2.job; JOIN ANSWER ================= ID NAME JOB -- -------- ----20 Pernal Sales 30 Marenghi Clerk 30 Marenghi Mgr STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Joins
168
DB2 9 Cookbook
ON vs. WHERE
WHERE checks used to filter rows WHERE checks always exclude non-matching rows ON checks define which rows match the join ON checks sometimes exclude rows, sometimes not
Examples
SELECT * FROM staff_v1 v1 LEFT OUTER JOIN staff_v2 v2 ON 1 = 1 AND v1.id = v2.id ORDER BY v1.id ,v2.job; ANSWER ==================== ID NAME ID JOB -- -------- -- ----10 Sanders - 20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
SELECT * FROM staff_v1 v1 LEFT OUTER JOIN staff_v2 v2 ON 1 = 1 WHERE v1.id = v2.id ORDER BY v1.id ,v2.job;
ANSWER ==================== ID NAME ID JOB -- -------- -- ----20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr
Joins
169
DB2 9 Cookbook
Inner Join (1 of 2)
STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+ INNER-JOIN ANSWER ==================== ID NAME ID JOB -- -------- -- ----20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr
=========>
Notes
Only matching rows (in both tables) are returned ON and WHERE check work the same way Can do Cartesian Product
Joins
170
DB2 9 Cookbook
Inner Join (2 of 2)
Style #1
SELECT FROM * staff_v1 v1 ,staff_v2 v2 WHERE v1.id = v2.id ORDER BY v1.id ,v2.job; ANSWER ==================== ID NAME ID JOB -- -------- -- ----20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Style #2
SELECT * FROM staff_v1 v1 INNER JOIN staff_v2 v2 ON v1.id = v2.id ORDER BY v1.id ,v2.job; ANSWER ==================== ID NAME ID JOB -- -------- -- ----20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr
Joins
171
DB2 9 Cookbook
=========>
Style #2
SELECT * FROM staff_v1 v1 LEFT OUTER JOIN staff_v2 v2 ON v1.id = v2.id ORDER BY 1,4;
In English
Select rows in 1st table Join to matching rows in 2nd table If no matching row, return null field
Joins
172
DB2 9 Cookbook
Style #1
SELECT FROM WHERE UNION SELECT v1.* ,v2.* staff_v1 v1 ,staff_v2 v2 v1.id = v2.id <== This join gets all rows in STAFF_V1 that match rows in STAFF_V2.
v1.* ,CAST(NULL AS SMALLINT) AS id ,CAST(NULL AS CHAR(5)) AS job FROM staff_v1 v1 WHERE v1.id NOT IN (SELECT id FROM staff_v2) ORDER BY 1,4;
<== This query gets all the rows in STAFF_V1 with no matching rows in STAFF_V2.
Joins
173
DB2 9 Cookbook
ON Check on TO table
SELECT * FROM staff_v1 v1 LEFT OUTER JOIN staff_v2 v2 ON v1.id = v2.id AND v2.job <> 'Mgr' ORDER BY v1.id ,v2.job; ANSWER ==================== ID NAME ID JOB -- -------- -- ----10 Sanders - 20 Pernal 20 Sales 30 Marenghi 30 Clerk
Joins
174
DB2 9 Cookbook
=========>
Style #2
SELECT * FROM staff_v1 v1 RIGHT OUTER JOIN staff_v2 v2 ON v1.id = v2.id ORDER BY v2.id ,v2.job;
In English
Select rows in 2nd table Join to matching rows in 1st table If no matching row, return null field
Joins
175
DB2 9 Cookbook
Style #1
SELECT FROM WHERE UNION SELECT v1.* ,v2.* staff_v1 v1 ,staff_v2 v2 v1.id = v2.id ANSWER ==================== ID NAME ID JOB -- -------- -- ----20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr - 40 Sales - 50 Mgr
CAST(NULL AS SMALLINT) AS id ,CAST(NULL AS VARCHAR(9)) AS name ,v2.* FROM staff_v2 v2 WHERE v2.id NOT IN (SELECT id FROM staff_v1) ORDER BY 3,4;
Joins
176
DB2 9 Cookbook
=========>
Style #2
SELECT * FROM staff_v1 v1 FULL OUTER JOIN staff_v2 v2 ON v1.id = v2.id ORDER BY v1.id ,v2.id ,v2.job;
In English
Select rows from both tables Join rows on match field(s).
Joins
177
DB2 9 Cookbook
Style #1
SELECT FROM WHERE UNION SELECT v1.* ,v2.* staff_v1 v1 ,staff_v2 v2 v1.id = v2.id v1.* ,CAST(NULL AS SMALLINT) AS id ,CAST(NULL AS CHAR(5)) AS job staff_v1 v1 v1.id NOT IN (SELECT ID FROM STAFF_V2)
CAST(NULL AS SMALLINT) AS id ,CAST(NULL AS VARCHAR(9)) AS name ,v2.* FROM staff_v2 v2 WHERE v2.id NOT IN (SELECT id FROM staff_v1) ORDER BY 1,3;
STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Joins
178
DB2 9 Cookbook
Examples
SELECT * FROM staff_v1 v1 FULL OUTER JOIN staff_v2 v2 ON v1.id = v2.id ORDER BY v1.id ,v2.id ,v2.job; ANSWER ==================== ID NAME ID JOB -- -------- -- ----10 Sanders - 20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr - 40 Sales - 50 Mgr
SELECT * FROM staff_v1 v1 FULL OUTER JOIN staff_v2 v2 ON v1.id = v2.id AND v1.id > 20 ORDER BY v1.id ,v2.id ,v2.job;
ANSWER ==================== ID NAME ID JOB -- -------- -- ----10 Sanders - 20 Pernal - 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr - 20 Sales - 40 Sales - 50 Mgr
STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Joins
179
DB2 9 Cookbook
Example
SELECT * FROM staff_v1 v1 FULL JOIN staff_v2 v2 ON v1.id = v2.id WHERE v1.id = v2.id ORDER BY 1,3,4; ANSWER ==================== ID NAME ID JOB -- -------- -- ----20 Pernal 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Joins
180
DB2 9 Cookbook
Joins
181
DB2 9 Cookbook
Cartesian Product (1 of 2)
Each row is joined to every row in other table Is almost always the wrong answer
STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
=========>
CARTESIAN-PRODUCT ==================== ID NAME ID JOB -- -------- -- ----ID NAME ID JOB -- -------- -- ----10 Sanders 20 Sales 10 Sanders 30 Clerk 10 Sanders 30 Mgr 10 Sanders 40 Sales 10 Sanders 50 Mgr 20 Pernal 20 Sales 20 Pernal 30 Clerk 20 Pernal 30 Mgr 20 Pernal 40 Sales 20 Pernal 50 Mgr 30 Marenghi 20 Sales 30 Marenghi 30 Clerk 30 Marenghi 30 Mgr 30 Marenghi 40 Sales 30 Marenghi 50 Mgr
Joins
182
DB2 9 Cookbook
Cartesian Product (2 of 2)
Style #1
SELECT FROM * staff_v1 v1 ,staff_v2 v2 ORDER BY v1.id ,v2.id ,v2.job;
Style #2
SELECT * FROM staff_v1 v1 INNER JOIN staff_v2 v2 ON 'A' <> 'B' ORDER BY v1.id ,v2.id ,v2.job;
Joins
183
DB2 9 Cookbook
Sample Data
STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Warning
Beware of JOINS with GROUP BY or DISTINCT logic. There may be a Partial Cartesian Product
Joins
184
DB2 9 Cookbook
Sample Data
STAFF_V1 +-----------+ |ID|NAME | |--|--------| |10|Sanders | |20|Pernal | |30|Marenghi| +-----------+ STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Notes
Coalesce function returns first non-null value.
Joins
185
DB2 9 Cookbook
Example
SELECT v1.id ,v1.name ,(SELECT FROM WHERE FROM staff_v1 WHERE v1.id <> ORDER BY v1.id; v2.job staff_v2 v2 v1.id = v2.id) AS jb v1 30 ANSWER ================= ID NAME JB -- -------- ----10 Sanders 20 Pernal Sales
STAFF_V2 +---------+ |ID|JOB | |--|------| |20|Sales | |30|Clerk | |30|Mgr | |40|Sales | |50|Mgr | +---------+
Joins
186
DB2 9 Cookbook
Multiple Columns
SELECT v2.id ,v2.job ,(SELECT FROM WHERE ,(SELECT FROM WHERE FROM staff_v2 ORDER BY v2.id ,v2.job; v1.name staff_v1 v1 v2.id = v1.id) LENGTH(V1.NAME) AS N2 staff_v1 v1 v2.id = v1.id) v2 ANSWER ==================== ID JOB NAME N2 -- ----- -------- -20 Sales Pernal 6 30 Clerk Marenghi 8 30 Mgr Marenghi 8 40 Sales 50 Mgr -
Joins
187
DB2 9 Cookbook
Joins
188
DB2 9 Cookbook
Sub-Query
Sub-Query
189
DB2 9 Cookbook
INSERT INTO table1 VALUES ('A','AA'),('B','BB'),('C','CC'); INSERT INTO table2 VALUES ('A','A','A'),('B','A',NULL);
Sub-Query
190
DB2 9 Cookbook
Sub-Query Syntax
expression =, <, >, <>, etc SOME ANY ALL NOT EXISTS IN ( subselect )
Example
SELECT * FROM table1 WHERE t1a > ANY (SELECT t2a FROM table2);
Warning
Sub-queries return "true" or "false" Data can be "true", "false", or "unknown" Presence of null data and/or empty-sets greatly complicates sub-queries
Sub-Query
191
DB2 9 Cookbook
No Keyword Sub-Query (1 of 2)
Works
SELECT * FROM table1 WHERE t1a = (SELECT t2a FROM table2 WHERE t2a = 'A'); SUB-Q RESLT +---+ |T2A| |---| |A | +---+ TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ ANSWER ======= T1A T1B --- -A AA
Notes
If no row in the sub-query result matches, the answer is false If one row in the sub-query result matches, the answer is true If more than one row in the sub-query result matches, you get a SQL error
Sub-Query
192
DB2 9 Cookbook
No Keyword Sub-Query (2 of 2)
Fails
SELECT * FROM table1 WHERE t1a = (SELECT t2a FROM table2); SUB-Q RESLT +---+ |T2A| |---| |A | |B | +---+ TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ ANSWER ======= <error>
Notes
If no row in the sub-query result matches, the answer is false If one row in the sub-query result matches, the answer is true If more than one row in the sub-query result matches, you get a SQL error
Sub-Query
193
DB2 9 Cookbook
ANY Sub-Query
SELECT * FROM table1 WHERE t1a > ANY (SELECT t2a FROM table2); ANSWER ======= T1A T1B --- -B BB C CC SUB-Q RESLT +---+ |T2A| |---| |A | |B | +---+ TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
Notes
If any row in the sub-query result matches, the answer is true If the sub-query result is empty, or all nulls, the answer is false If no value found in the sub-query result matches, the answer is also false
Sub-Query
194
DB2 9 Cookbook
ALL Sub-Query (1 of 3)
With non-Empty Sub-Query Result
SELECT * FROM table1 WHERE t1a = ALL (SELECT t2b FROM table2 WHERE t2b >= 'A'); ANSWER ======= T1A T1B --- -A AA SUB-Q RESLT +---+ |T2B| |---| |A | |A | +---+
Notes
If all rows in the sub-query result match, the answer is true If there are no rows in the sub-query result, the answer is also true If any row in the sub-query result does not match, or is null, the answer is false
Sub-Query
195
DB2 9 Cookbook
ALL Sub-Query (2 of 3)
With Empty Sub-Query Result
SELECT * FROM table1 WHERE t1a = ALL (SELECT t2b FROM table2 WHERE t2b >= 'X'); ANSWER ======= T1A T1B --- -A AA B BB C CC SUB-Q RESLT +---+ |T2B| |---| +---+
Notes
If all rows in the sub-query result match, the answer is true If there are no rows in the sub-query result, the answer is also true If any row in the sub-query result does not match, or is null, the answer is false
Sub-Query
196
DB2 9 Cookbook
ALL Sub-Query (2 of 3)
With Extra Check for Empty Set
SELECT * FROM table1 WHERE t1a = ALL (SELECT t2b FROM table2 WHERE t2b >= 'X') AND 0 <> (SELECT COUNT(*) FROM table2 WHERE t2b >= 'X'); ANSWER ====== 0 rows SQ-#1 RESLT +---+ |T2B| |---| +---+ SQ-#2 RESLT +---+ |(*)| |---| |0 | +---+ TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
Notes
1st sub-query: Check that all rows equal value 2nd sub-query: Check that there is more than zero values
Sub-Query
197
DB2 9 Cookbook
EXISTS Sub-Query (1 of 2)
Always Returns a Match
SELECT * FROM table1 WHERE EXISTS (SELECT * FROM table2); ANSWER ======= T1A T1B --- -A AA B BB C CC TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
Notes
If the sub-query matches on one or more rows, the result is true If the sub-query matches on no rows, the result is false
Sub-Query
198
DB2 9 Cookbook
EXISTS Sub-Query (2 of 2)
Always returns a match (error)
SELECT * FROM table1 WHERE EXISTS (SELECT COUNT(*) FROM table2 WHERE t2b = 'X'); ANSWER ======= T1A T1B --- -A AA B BB C CC TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
Notes
Even when no data found in sub-query, COUNT(*) has value - zero Do not code this sub-query
Sub-Query
199
DB2 9 Cookbook
SELECT * FROM table1 WHERE t1a = ALL (SELECT t2c FROM table2 WHERE t2c >= 'A');
Notes
Compare ALL and NOT EXISTS to return true when all matching rows equal In above examples, data found = return matching TABLE1 rows Both results equal
Sub-Query
200
DB2 9 Cookbook
SELECT * FROM table1 WHERE t1a = ALL (SELECT t2c FROM table2 WHERE t2c >= 'X');
Notes
In above examples, sub-query empty set = return all TABLE1 rows Both results equal
Sub-Query
201
DB2 9 Cookbook
SELECT * FROM table1 WHERE t1a = ALL (SELECT t2c FROM table2);
Notes
NOT EXISTS sub-query does not see null - returns true ALL sub-query sees null - returns false
Sub-Query
202
DB2 9 Cookbook
IN Sub-Query
Two Matches
SELECT * FROM table1 WHERE t1a IN (SELECT t2a FROM table2); ANSWER ======= T1A T1B --- -A AA B BB TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
No Matches
SELECT * FROM table1 WHERE t1a IN (SELECT t2a FROM table2 WHERE t2a >= 'X'); ANSWER ====== 0 rows
Notes
If any row in the sub-query result matches, the answer is true. If the sub-query result is empty, the answer is false. If no row in the sub-query result matches, the answer is also false. If all of the values in the sub-query result are null, the answer is false.
Sub-Query
203
DB2 9 Cookbook
Notes
Nulls in "top" table results in no match (null does not equal null)
Sub-Query
204
DB2 9 Cookbook
NOT IN Sub-Query
No Matches
SELECT * FROM table1 WHERE t1a NOT IN (SELECT t2c FROM table2); ANSWER ====== 0 rows TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
Matches
SELECT * FROM table1 WHERE t1a NOT IN (SELECT t2c FROM table2 WHERE t2c IS NOT NULL); ANSWER ======= T1A T1B --- -B BB C CC
Notes
True if no match found False if match found False if null found - to get true, eliminate from check
Sub-Query
205
DB2 9 Cookbook
Notes
Finds non-matching rows while ignoring nulls Compare to previous foil
Sub-Query
206
DB2 9 Cookbook
Sub-Query Flavours
Uncorrelated Sub-Query
SELECT * FROM table1 WHERE t1a IN (SELECT t2a FROM table2); ANSWER ======= T1A T1B --- -A AA B BB TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
Correlated Sub-Query
SELECT * FROM table1 WHERE t1a IN (SELECT t2a FROM table2 WHERE t1a = t2a); ANSWER ======= T1A T1B --- -A AA B BB
Notes
Uncorrelated: Correlated: Can resolve sub-query without reference to "top" table Uses current value in "top" table resolve sub-query
Sub-Query
207
DB2 9 Cookbook
Notes
Need to use correlation names when field names not unique Can make the SQL easier to read Does not affect access path
Sub-Query
208
DB2 9 Cookbook
Multi-field Sub-Queries
Equal Checks
SELECT * FROM table1 WHERE (t1a,t1b) IN (SELECT t2a, t2b FROM table2); ANSWER ====== 0 rows TABLE1 +-------+ |T1A|T1B| |---|---| |A |AA | |B |BB | |C |CC | +-------+ TABLE2 +-----------+ |T2A|T2B|T2C| |---|---|---| |A |A |A | |B |A | - | +-----------+ "-" = null
Non-Equal Checks
SELECT * FROM table1 WHERE EXISTS (SELECT FROM WHERE AND ANSWER ======= T1A T1B --- -A AA B BB
Sub-Query
209
DB2 9 Cookbook
Nested Sub-Queries
SELECT empno ,lastname ,salary FROM employee WHERE salary > (SELECT MAX(salary) FROM employee WHERE empno NOT IN (SELECT empno FROM emp_act WHERE projno LIKE 'MA%')) ORDER BY 1; ANSWER ========================= EMPNO LASTNAME SALARY ------ --------- -------000010 HAAS 52750.00 000110 LUCCHESSI 46500.00
Sub-Query
210
DB2 9 Cookbook
SELECT * FROM table1 t1 WHERE NOT EXISTS (SELECT * FROM table2 t2 WHERE t1.t1a = t2.t2c); SELECT * FROM table1 WHERE t1a NOT IN (SELECT t2c FROM table2 WHERE t2c IS NOT NULL);
DB2 9 Cookbook
t1 COUNT(*) table2 t2 t1.t1a = t2.t2c); ANSWER ======= T1A T1B --- --A AA
SELECT * FROM table1 WHERE t1a = ANY (SELECT t2c FROM table2); SELECT * FROM table1 WHERE t1a = SOME (SELECT t2c FROM table2); SELECT * FROM table1 WHERE t1a IN (SELECT t2c FROM table2);
Sub-Query
212
DB2 9 Cookbook
SELECT t1.* FROM table1 t1 ,(SELECT DISTINCT t2c FROM table2 )AS t2 WHERE t1.t1a = t2.t2c;
SELECT t1.* FROM TABLE1 t1 INNER JOIN (SELECT DISTINCT t2c FROM table2 ) AS t2 ON t1.t1a = t2.t2c;
Sub-Query
213
DB2 9 Cookbook
SELECT * FROM table1 WHERE (t1a,10) IN (SELECT t2b, COUNT(*) FROM table2 GROUP BY t2b);
Sub-Query
214
DB2 9 Cookbook
SELECT t1.* FROM table1 t1 ,(SELECT FROM GROUP BY HAVING ) AS T2 WHERE t1.t1a =
SELECT t1.* FROM table1 t1 INNER JOIN (SELECT FROM GROUP BY HAVING )AS t2 ON t1.t1a =
Sub-Query
215
DB2 9 Cookbook
Sub-Query
216
DB2 9 Cookbook
Notes
Above examples - empty set means sub-query returns true
Sub-Query
217
DB2 9 Cookbook
Notes
Above examples - empty set means sub-query returns false
Sub-Query
218
DB2 9 Cookbook
219
DB2 9 Cookbook
220
DB2 9 Cookbook
Warning
EXCEPT differs from UNION and INTERSECT - not commutative
221
DB2 9 Cookbook
Sample Views
CREATE VIEW AS VALUES CREATE VIEW AS VALUES R1 (R1) ('A'),('A'),('A'),('B'),('B'),('C'),('C'),('C'),('E'); R2 (R2) ('A'),('A'),('B'),('B'),('B'),('C'),('D'); ANSWER ====== SELECT R1 R1 R2 FROM R1 -- -ORDER BY R1; A A A A SELECT R2 A B FROM R2 B B ORDER BY R2; B B C C C D C E
222
DB2 9 Cookbook
Notes
UNION returns all values UNION ALL returns all rows
223
DB2 9 Cookbook
Notes
INTERSECT returns intersecting values INTERSECT ALL returns all intersecting rows
224
DB2 9 Cookbook
Notes
EXCEPT returns non-intersecting values EXCEPT ALL returns all non-intersecting rows
225
DB2 9 Cookbook
Notes
EXCEPT and EXCEPT ALL return different result - depending upon the sequence in which tables are listed
226
DB2 9 Cookbook
ANSWER ====== E
ANSWER ====== E
ANSWER ====== A B C E
Precedence Rules
Operations in parenthesis done first INTERSECT operations done before either UNION or EXCEPT Operations of equal worth done from top to bottom
227
DB2 9 Cookbook
228
DB2 9 Cookbook
229
DB2 9 Cookbook
Warning
EXCEPT differs from UNION and INTERSECT - not commutative
230
DB2 9 Cookbook
Sample Views
CREATE VIEW AS VALUES CREATE VIEW AS VALUES R1 (R1) ('A'),('A'),('A'),('B'),('B'),('C'),('C'),('C'),('E'); R2 (R2) ('A'),('A'),('B'),('B'),('B'),('C'),('D'); ANSWER ====== SELECT R1 R1 R2 FROM R1 -- -ORDER BY R1; A A A A SELECT R2 A B FROM R2 B B ORDER BY R2; B B C C C D C E
231
DB2 9 Cookbook
Notes
UNION returns all values UNION ALL returns all rows
232
DB2 9 Cookbook
Notes
INTERSECT returns intersecting values INTERSECT ALL returns all intersecting rows
233
DB2 9 Cookbook
Notes
EXCEPT returns non-intersecting values EXCEPT ALL returns all non-intersecting rows
234
DB2 9 Cookbook
Notes
EXCEPT and EXCEPT ALL return different result - depending upon the sequence in which tables are listed
235
DB2 9 Cookbook
ANSWER ====== E
ANSWER ====== E
ANSWER ====== A B C E
Precedence Rules
Operations in parenthesis done first INTERSECT operations done before either UNION or EXCEPT Operations of equal worth done from top to bottom
236
DB2 9 Cookbook
237
DB2 9 Cookbook
Example
CREATE TABLE invoice_data (invoice# INTEGER NOT GENERATED ALWAYS AS IDENTITY (START WITH 1 ,INCREMENT BY 1 ,NO MAXVALUE ,NO CYCLE ,ORDER) ,sale_date DATE NOT ,customer_id CHAR(20) NOT ,product_id INTEGER NOT ,quantity INTEGER NOT ,price DECIMAL(18,2) NOT ,PRIMARY KEY (invoice#)); NULL
238
DB2 9 Cookbook
ALWAYS BY DEFAULT
numeric constant
numeric constant
numeric constant
DB2 9 Cookbook
Example
CREATE TABLE invoice_data (invoice# INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 ,INCREMENT BY 1 ,NO MAXVALUE ,NO CYCLE ,ORDER) ,sale_date DATE NOT NULL ,customer_id CHAR(20) NOT NULL ,PRIMARY KEY (invoice#));
240
DB2 9 Cookbook
241
DB2 9 Cookbook
Sequence Examples (1 of 2)
Ascending Sequence
CREATE TABLE test_data (key# SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY ,dat1 SMALLINT NOT NULL ,ts1 TIMESTAMP NOT NULL ,PRIMARY KEY(key#)); KEY# FIELD - VALUES ASSIGNED ============================ 1 2 3 4 5 6 7 8 9 10 11 etc.
Descending Sequence
CREATE TABLE test_data (key# SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 6 ,INCREMENT BY -3 ,NO CYCLE ,NO CACHE ,ORDER) ,dat1 SMALLINT NOT NULL ,ts1 TIMESTAMP NOT NULL ,PRIMARY KEY(key#)); KEY# FIELD - VALUES ASSIGNED ============================ 6 3 0 -3 -6 -9 -12 -15 etc.
242
DB2 9 Cookbook
Sequence Examples (2 of 2)
Sequence Goes Nowhere
CREATE TABLE test_data (key# SMALLINT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 123 ,MAXVALUE 124 ,INCREMENT BY 0 ,NO CYCLE ,NO ORDER) ,dat1 SMALLINT NOT NULL ,ts1 TIMESTAMP NOT NULL); KEY# VALUES ASSIGNED ============================ 123 123 123 123 123 123 etc.
243
DB2 9 Cookbook
Insert Usage
CREATE TABLE invoice_data (invoice# INTEGER NOT GENERATED ALWAYS AS IDENTITY (START WITH 100 ,INCREMENT BY 1 ,NO CYCLE ,ORDER) ,sale_date DATE NOT ,customer_id CHAR(20) NOT ,product_id INTEGER NOT ,quantity INTEGER NOT ,price DECIMAL(18,2) NOT ,PRIMARY KEY (invoice#)); NULL
Insert Rows
INSERT INTO invoice_data VALUES (DEFAULT,'2001-11-22','ABC',123,100,10); INSERT INTO invoice_data (SALE_DATE,CUSTOMER_ID,PRODUCT_ID,QUANTITY,PRICE) VALUES ('2001-11-23','DEF',123,100,10);
After Inserts
INVOICE# -------100 101 SALE_DATE ---------11/22/2001 11/23/2001 CUSTOMER_ID ----------ABC DEF PRODUCT_ID ---------123 123 QUANTITY -------100 100 PRICE ----10.00 10.00
244
DB2 9 Cookbook
Warning
Altering start number or increment can cause duplicate values
245
DB2 9 Cookbook
Gaps in Sequence
CREATE TABLE customers (cust# INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (NO CACHE) ,cname CHAR(10) NOT NULL ,ctype CHAR(03) NOT NULL ,PRIMARY KEY (cust#)); COMMIT;
INSERT INTO customers VALUES (DEFAULT,'FRED','XXX'); SELECT * FROM customers ORDER BY 1; ROLLBACK; <<< ANSWER =================== CUST# CNAME CTYPE ----- ----- ----1 FRED XXX
INSERT INTO customers VALUES (DEFAULT,'FRED','XXX'); SELECT * FROM customers ORDER BY 1; COMMIT; <<< ANSWER =================== CUST# CNAME CTYPE ----- ----- ----2 FRED XXX
Notes
Gaps can occur if an insert is subsequently rolled out
Identity Columns and Sequences 246
DB2 9 Cookbook
IDENTITY_VAL_LOCAL Function (1 of 2)
INSERT INTO invoice_table VALUES (DEFAULT,'2000-11-22','ABC',123,100,10); WITH temp (id) AS (VALUES (IDENTITY_VAL_LOCAL())) SELECT * FROM temp; COMMIT; WITH temp (id) AS (VALUES (IDENTITY_VAL_LOCAL())) SELECT * FROM temp; <<< ANSWER ====== ID ---1 <<< ANSWER ====== ID ---1
Notes
Returns Identity Column value for most recent single-row insert by user Returns null if no insert done in thread (or Unit of Work?) If inserts done to multiple tables, returns value from last insert done Cannot be called from a trigger - use transition variable for column instead Value returned is unpredictable if prior insert failed Warning: Ignores multi-row inserts
247
DB2 9 Cookbook
IDENTITY_VAL_LOCAL Function (2 of 2)
INSERT INTO invoice_table VALUES (DEFAULT,'2000-11-23','ABC',123,100,10); INSERT INTO invoice_table VALUES (DEFAULT,'2000-11-24','ABC',123,100,10) ,(DEFAULT,'2000-11-25','ABC',123,100,10); SELECT invoice# AS inv# ,sale_date ,IDENTITY_VAL_LOCAL() AS id FROM invoice_table ORDER BY 1; COMMIT;
ANSWER ================== INV# SALE_DATE ID ---- ---------- -1 11/22/2000 2 2 11/23/2000 2 3 11/24/2000 2 4 11/25/2000 2
248
DB2 9 Cookbook
IDENTITY_VAL_LOCAL Function (2 of 2)
Ignores Multi-row Inserts
INSERT INTO invoice_table VALUES (DEFAULT,'2000-11-23','ABC',123,100,10); INSERT INTO invoice_table VALUES (DEFAULT,'2000-11-24','ABC',123,100,10) ,(DEFAULT,'2000-11-25','ABC',123,100,10); SELECT invoice# AS inv# ,sale_date ,IDENTITY_VAL_LOCAL() AS id FROM invoice_table ORDER BY 1; COMMIT;
ANSWER ================== INV# SALE_DATE ID ---- ---------- -1 11/22/2000 2 2 11/23/2000 2 3 11/24/2000 2 4 11/25/2000 2
Usage in Predicate
SELECT invoice# AS inv# ,sale_date ,IDENTITY_VAL_LOCAL() AS id FROM invoice_table WHERE id = IDENTITY_VAL_LOCAL(); ANSWER ================== INV# SALE_DATE ID ---- ---------- -2 11/23/2000 2
249
DB2 9 Cookbook
Sequences - Introduction
Notes
Similar to Identity Column, but not tied to any table Different functions used to get last/next value Can be used on multiple tables
Example
CREATE SEQUENCE fred AS DECIMAL(31) START WITH 100 INCREMENT BY 2 NO MINVALUE NO MAXVALUE NO CYCLE CACHE 20 ORDER; SEQ# VALUES ASSIGNED ==================== 100 102 104 106 etc.
250
DB2 9 Cookbook
Sequences - Usage
Notes
There is no current sequence value To use in INSERT or UPDATE, have to use trigger Selecting NEXT value (in query), increments the sequence
Example
CREATE SEQUENCE fred; COMMIT; WITH temp1 (n1) AS (VALUES 1 UNION ALL SELECT n1 + 1 FROM temp1 WHERE n1 < 5 ) SELECT NEXTVAL FOR fred AS seq# FROM temp1; ANSWER ====== SEQ# ---1 2 3 4 5
251
DB2 9 Cookbook
SQL Usage
SELECT - as long as no DISTINCT, GROUP BY, UNION, etc. INSERT - with restrictions UPDATE - with restrictions SET host variable NEXTVAL only can be used in trigger
Not Allowed
CASE statement Join condition of join Most sub-queries
Identity Columns and Sequences 252
DB2 9 Cookbook
===>
NXT --1
===>
PRV --1
===>
253
DB2 9 Cookbook
===>
254
DB2 9 Cookbook
Multi-table Usage (1 of 2)
CREATE SEQUENCE cust# START WITH 1 INCREMENT BY 1 NO MAXVALUE NO CYCLE ORDER; CREATE TABLE us_customer (cust# INTEGER ,cname CHAR(10) ,frst_sale DATE ,#sales INTEGER ,PRIMARY KEY (cust#));
CREATE TRIGGER us_cust_ins NO CASCADE BEFORE INSERT ON us_customer REFERENCING NEW AS nnn FOR EACH ROW MODE DB2SQL SET nnn.cust# = NEXTVAL FOR cust#; CREATE TABLE intl_customer (cust# INTEGER ,cname CHAR(10) ,frst_sale DATE ,#sales INTEGER ,PRIMARY KEY (cust#));
CREATE TRIGGER intl_cust_ins NO CASCADE BEFORE INSERT ON intl_customer REFERENCING NEW AS nnn FOR EACH ROW MODE DB2SQL SET nnn.cust# = NEXTVAL FOR cust#;
255
DB2 9 Cookbook
Multi-table Usage (2 of 2)
INSERT INTO us_customer (cname, frst_sale, #sales) VALUES ('FRED','2002-10-22',1) ,('JOHN','2002-10-23',1); INSERT INTO intl_customer (cname, frst_sale, #sales) VALUES ('SUE','2002-11-12',2) ,('DEB','2002-11-13',2); COMMIT; ANSWERS ============================= CUST# CNAME FRST_SALE #SALES ----- ----- ---------- -----1 FRED 10/22/2002 1 2 JOHN 10/23/2002 1
CUST# ----3 4
256
DB2 9 Cookbook
Counting Deletes
CREATE SEQUENCE delete_rows START WITH 1 INCREMENT BY 1 NO MAXVALUE NO CYCLE; CREATE SEQUENCE delete_stmts START WITH 1 INCREMENT BY 1 NO MAXVALUE NO CYCLE; CREATE TABLE customer (cust# INTEGER ,cname CHAR(10) ,frst_sale DATE ,#sales INTEGER ,PRIMARY KEY (cust#));
CREATE TRIGGER cust_del_rows AFTER DELETE ON customer FOR EACH ROW MODE DB2SQL WITH temp1 (n1) AS (VALUES(1)) SELECT NEXTVAL FOR delete_rows FROM temp1; CREATE TRIGGER cust_del_stmts AFTER DELETE ON customer FOR EACH STATEMENT MODE DB2SQL WITH temp1 (n1) AS (VALUES(1)) SELECT NEXTVAL FOR delete_stmts FROM temp1;
257
DB2 9 Cookbook
Related Functions
IDENTITY_VAL_LOCAL returns null if no prior value PREVVAL gets SQL error if no prior value IDENTITY_VAL_LOCAL ignores multi-row inserts One cannot tell which table IDENTITY_VAL_LOCAL value refers to
258
DB2 9 Cookbook
Temporary Tables
Temporary Tables
259
DB2 9 Cookbook
Characteristics
Not shareable by multiple users Data not recoverable
Example
SELECT FROM id ,salary (SELECT s.* ,ROW_NUMBER() OVER(ORDER BY salary DESC) AS sorder staff s id < 200
FROM WHERE )AS xxx WHERE sorder BETWEEN 2 AND 3 ORDER BY id;
Temporary Tables
260
DB2 9 Cookbook
Temporary Tables
261
DB2 9 Cookbook
dept AS dept# ,MAX(salary) AS max_sal FROM staff WHERE dept < 50 GROUP BY dept )AS staff_dept ON dept = dept# WHERE name LIKE 'S%' ORDER BY id;
Temporary Tables
262
DB2 9 Cookbook
Temporary Tables
263
DB2 9 Cookbook
Temporary Tables
264
DB2 9 Cookbook
dept ,AVG(salary) AS avg_sal FROM staff GROUP BY dept), temp2 AS (SELECT MAX(avg_sal) AS max_avg FROM temp1) SELECT * FROM temp2;
Temporary Tables
265
DB2 9 Cookbook
Temporary Tables
266
DB2 9 Cookbook
Temporary Tables
267
DB2 9 Cookbook
Temporary Tables
268
DB2 9 Cookbook
Full-Select (1 of 6)
Nested full-selects
SELECT id FROM (SELECT * FROM (SELECT id, years, salary FROM (SELECT * FROM (SELECT * FROM staff WHERE dept < 77 )AS t1 WHERE id < 300 )AS t2 WHERE job LIKE 'C%' )AS t3 WHERE salary < 18000 )AS t4 WHERE years < 5; ANSWER ====== ID --170 180 230
Notes
Full-selects can be used in FROM or SELECT part of query If in FROM part of query, table must have a correlation name If in SELECT part of query, must one return only one value per row
Temporary Tables
269
DB2 9 Cookbook
Full-Select (2 of 6)
Join full-select to real table
SELECT a.id ,a.dept ,a.salary ,DEC(b.avgsal,7,2) AS FROM staff a LEFT OUTER JOIN (SELECT dept ,AVG(salary) FROM staff GROUP BY dept HAVING AVG(salary) )AS b ON a.dept = b.dept WHERE a.id < 40 ORDER BY a.id; ANSWER ========================= ID DEPT SALARY AVG_DEPT -- ---- -------- -------10 20 18357.50 16071.52 20 20 18171.25 16071.52 30 38 17506.75 -
avg_dept
AS dept AS avgsal
> 16000
Temporary Tables
270
DB2 9 Cookbook
Full-Select (3 of 6)
Full-Select with external table reference
SELECT a.id ,a.dept ,a.salary ,b.deptsal FROM staff a ,TABLE (SELECT b.dept ,SUM(b.salary) AS deptsal FROM staff b WHERE b.dept = a.dept GROUP BY b.dept )AS b WHERE a.id < 40 ORDER BY a.id; ANSWER ========================= ID DEPT SALARY DEPTSAL -- ---- -------- -------10 20 18357.50 64286.10 20 20 18171.25 64286.10 30 38 17506.75 77285.55
ANSWER ========================= ID DEPT SALARY DEPTSAL -- ---- -------- -------10 20 18357.50 64286.10 20 20 18171.25 64286.10 30 38 17506.75 77285.55
271
DB2 9 Cookbook
Full-Select (4 of 6)
Use an uncorrelated Full-Select in a SELECT list
SELECT id ,salary ,(SELECT MAX(salary) FROM staff )AS maxsal FROM staff a WHERE id < 60 ORDER BY id; ANSWER ==================== ID SALARY MAXSAL -- -------- -------10 18357.50 22959.20 20 18171.25 22959.20 30 17506.75 22959.20 40 18006.00 22959.20 50 20659.80 22959.20
Temporary Tables
272
DB2 9 Cookbook
Full-Select (5 of 6)
Use correlated and uncorrelated Full-Selects in a SELECT list
SELECT id ,dept ,salary ,(SELECT MAX(salary) FROM staff b WHERE b.dept = a.dept) ,(SELECT MAX(salary) FROM staff) FROM staff a WHERE id < 60 ORDER BY id; ANSWER ================================== ID DEPT SALARY 4 5 -- ---- -------- -------- -------10 20 18357.50 18357.50 22959.20 20 20 18171.25 18357.50 22959.20 30 38 17506.75 18006.00 22959.20 40 38 18006.00 18006.00 22959.20 50 15 20659.80 20659.80 22959.20
Full-select in INSERT
INSERT INTO staff SELECT id + 1 ,(SELECT MIN(name) FROM staff) ,(SELECT dept FROM staff s2 WHERE s2.id = s1.id - 100) ,'A',1,2,3 FROM staff s1 WHERE id = (SELECT MAX(id) FROM staff);
Temporary Tables
273
DB2 9 Cookbook
Full-Select (6 of 6)
Full-Select (uncorrelated), gives all workers AVG salary (+$2000)
UPDATE staff a SET salary = (SELECT AVG(salary)+ 2000 FROM staff) WHERE id < 60; ANSWER: ======= ID DEPT -- ---10 20 20 20 30 38 40 38 50 15 SALARY ================= BEFORE AFTER -------- -------18357.50 18675.64 18171.25 18675.64 17506.75 18675.64 18006.00 18675.64 20659.80 18675.64
Full-Select (correlated) gives all workers AVG salary per department (+$2000)
UPDATE staff a SET salary = (SELECT AVG(salary) + 2000 FROM staff b WHERE a.dept = b.dept ) WHERE id < 60; ANSWER: ======= ID DEPT -- ---10 20 20 20 30 38 40 38 50 15 SALARY ================= BEFORE AFTER -------- -------18357.50 18071.52 18171.25 18071.52 17506.75 17457.11 18006.00 17457.11 20659.80 17482.33
Temporary Tables
274
DB2 9 Cookbook
( LIKE AS (
column-definition
DEFINITION ONLY
INCLUDING EXCLUDING
COLUMN
DEFAULTS
Notes
Defines temporary table
Temporary Tables
275
DB2 9 Cookbook
Warning
Do not use to store temporary output when doing updates
Temporary Tables
276
DB2 9 Cookbook
Temporary Tables
277
DB2 9 Cookbook
Recursive SQL
Recursive SQL
278
DB2 9 Cookbook
Recursive SQL
279
DB2 9 Cookbook
<
Processing Logic
HIERARCHY +---------------+ |PKEY |CKEY |NUM| |-----|-----|---| |AAA |BBB | 1| |AAA |CCC | 5| |AAA |DDD | 20| |CCC |EEE | 33| |DDD |EEE | 44| |DDD |FFF | 5| |FFF |GGG | 5| +---------------+
PKEY > CKEY > AAA BBB AAA CCC AAA DDD CCC EEE DDD EEE DDD FFF FFF GGG
Recursive SQL
280
DB2 9 Cookbook
Sample Data
HIERARCHY +---------------+ |PKEY |CKEY |NUM| |-----|-----|---| |AAA |BBB | 1| |AAA |CCC | 5| |AAA |DDD | 20| |CCC |EEE | 33| |DDD |EEE | 44| |DDD |FFF | 5| |FFF |GGG | 5| +---------------+ AAA | +-----+-----+ | | | BBB CCC DDD | | +-+ +-+--+ | | | EEE FFF | | GGG
Recursive SQL
281
DB2 9 Cookbook
Sample Data
HIERARCHY +---------------+ |PKEY |CKEY |NUM| |-----|-----|---| |AAA |BBB | 1| |AAA |CCC | 5| |AAA |DDD | 20| |CCC |EEE | 33| |DDD |EEE | 44| |DDD |FFF | 5| |FFF |GGG | 5| +---------------+ AAA | +-----+-----+ | | | BBB CCC DDD | | +-+ +-+--+ | | | EEE FFF | | GGG
Recursive SQL
282
DB2 9 Cookbook
Recursive SQL
283
DB2 9 Cookbook
Recursive SQL
284
DB2 9 Cookbook
Sample Data
HIERARCHY +---------------+ |PKEY |CKEY |NUM| |-----|-----|---| |AAA |BBB | 1| |AAA |CCC | 5| |AAA |DDD | 20| |CCC |EEE | 33| |DDD |EEE | 44| |DDD |FFF | 5| |FFF |GGG | 5| +---------------+ AAA | +-----+-----+ | | | BBB CCC DDD | | +-+ +-+--+ | | | EEE FFF | | GGG
Recursive SQL
285
DB2 9 Cookbook
Warning
Example selects rows where level < 3 Statement may run forever if hierarchy has loop Inefficient - Resolves whole hierarchy, then discards rows
Recursive SQL
286
DB2 9 Cookbook
Better Design
Example selects rows where level < 3 Statement doesn't run forever if loop in hierarchy Efficient - Stops processing when "n" levels reached
Recursive SQL
287
DB2 9 Cookbook
Processing Logic
Get start row Use recursion to get all rows, LEVEL <= 2 In final query, select only LEVEL = 2
Recursive SQL
288
DB2 9 Cookbook
Recursive SQL
289
DB2 9 Cookbook
Without Warning
WITH temp1 (n1) AS (SELECT INT(id) FROM staff WHERE id = 10 UNION ALL SELECT n1 +10 FROM temp1 WHERE n1 < 50 ) SELECT * FROM temp1; ANSWER ====== N1 -10 20 30 40 50
Recursive SQL
290
DB2 9 Cookbook
Hierarchy Flavours
DIVERGENT ========= AAA | +-+-+ | | BBB CCC | +-+-+ | | DDD EEE CONVERGENT ========== AAA | +-+-+ | | BBB CCC | | +-+-+-+ | | DDD EEE RECURSIVE ========= AAA<--+ | | +-+-+ | | | | BBB CCC>+ | +-+-+ | | DDD EEE BALANCED ======== AAA | +-+-+ | | BBB CCC | | | +---+ | | | DDD EEE FFF UNBALANCED ========== AAA | +-+-+ | | BBB CCC | +-+-+ | | DDD EEE
Recursive SQL
291
DB2 9 Cookbook
Recursive SQL
292
DB2 9 Cookbook
Recursive SQL
293
DB2 9 Cookbook
Recursive SQL
294
DB2 9 Cookbook
Recursive SQL
295
DB2 9 Cookbook
CREATE UNIQUE INDEX tble_x1 ON trouble (pkey, ckey); CREATE UNIQUE INDEX tble_x2 ON trouble (ckey, pkey);
Recursive SQL
296
DB2 9 Cookbook
Recursive SQL
297
DB2 9 Cookbook
Notes
The function scans a TEXT string, a block at a time, looking for matches Can be used to see if recursive SQL has been to same key value before
Recursive SQL 298
DB2 9 Cookbook
ANSWER =============================== PKEY CKEY LVL PATH LOOP ---- ---- --- ------------ ---AAA AAA 0 AAA 0 AAA BBB 1 AAABBB 0 AAA CCC 1 AAACCC 0 AAA DDD 1 AAADDD 0 CCC EEE 2 AAACCCEEE 0 DDD AAA 2 AAADDDAAA 1 DDD EEE 2 AAADDDEEE 0 DDD FFF 2 AAADDDFFF 0 AAA BBB 3 AAADDDAAABBB 0 AAA CCC 3 AAADDDAAACCC 0 AAA DDD 3 AAADDDAAADDD 2 FFF GGG 3 AAADDDFFFGGG 0
Notes
Recursive SQL builds string of compound keys. Query stops after "n" levels processed
This row ===> points back to the hierarchy parent.
TROUBLE +---------+ |PKEY|CKEY| |----|----| |AAA |BBB | |AAA |CCC | |AAA |DDD | |CCC |EEE | |DDD |AAA | |DDD |FFF | |DDD |EEE | |FFF |GGG | +---------+
AAA <------+ | | +-----+-----+ | | | | | BBB CCC DDD>-+ | | +-+ +-+--+ | | | EEE FFF | | GGG
Recursive SQL
299
DB2 9 Cookbook
Notes
Recursive SQL builds compound string of key values processed LOCATE_BLOCK function checks compound string against current key If LOCATE_BLOCK function finds match, recursion is not continued for that value
Recursive SQL 300
DB2 9 Cookbook
Notes
Same as previous SQL, but instead of excluding loops for answer, marks then as such, then goes no further
Recursive SQL
301
DB2 9 Cookbook
302
DB2 9 Cookbook
303
DB2 9 Cookbook
Answer
C0 ---0 10 20 30 40 50 60 70 80 90 C1 ---1 11 21 31 41 51 61 71 81 91 C2 ---2 12 22 32 42 52 62 72 82 92 C3 ---3 13 23 33 43 53 63 73 83 93 C4 ---4 14 24 34 44 54 64 74 84 94 C5 ---5 15 25 35 45 55 65 75 85 95 C6 ---6 16 26 36 46 56 66 76 86 96 C7 ---7 17 27 37 47 57 67 77 87 97 C8 ---8 18 28 38 48 58 68 78 88 98 C9 ---9 19 29 39 49 59 69 79 89 99
304
DB2 9 Cookbook
Answer
C0 ---0 10 20 30 40 50 60 70 80 90 C1 ---1 11 21 31 41 51 61 71 81 91 C2 ---2 12 22 32 42 52 62 72 82 92 C3 ---3 13 23 33 43 53 63 73 83 93 C4 ---4 14 24 34 44 54 64 74 84 94 C5 ---5 15 25 35 45 55 65 75 85 95 C6 ---6 16 26 36 46 56 66 76 86 96 C7 ---7 17 27 37 47 57 67 77 87 97 C8 ---8 18 28 38 48 58 68 78 88 98 C9 ---9 19 29 39 49 59 69 79 89 99
305
DB2 9 Cookbook
Note
If initial RAND seeded, gets reproducible results
306
DB2 9 Cookbook
AS AS AS AS
307
DB2 9 Cookbook
AS AS AS AS
308
DB2 9 Cookbook
309
DB2 9 Cookbook
310
DB2 9 Cookbook
311
DB2 9 Cookbook
312
DB2 9 Cookbook
ANSWER ======================= KEYCOL GAP_DT GSIZE ------ ---------- ----AAA 10/05/1995 1 AAA 10/08/1995 7 AAA 10/09/1995 6 AAA 10/10/1995 5 AAA 10/11/1995 4 AAA 10/12/1995 3 AAA 10/13/1995 2 AAA 10/14/1995 1 BBB 10/02/1995 1
313
DB2 9 Cookbook
314
DB2 9 Cookbook
Notes
First uses TRANSLATE to convert all digits to blanks Then uses LTRIM to remove blanks Then checks that output length is zero
Be Warned
Above solution does not work if data has decimal point, sign characters, etc. Use user-defined function (see notes) for more complex conversions
Fun with SQL 315
DB2 9 Cookbook
Answer
COL1 ---12 22 33 16 0 44 15 15 PRETTY_CHART -------------------------------------------------************ ********************** ********************************* **************** ******************************************** *************** ***************
316
DB2 9 Cookbook
FROM
317
DB2 9 Cookbook
Answer
CAT --1ST 2ND 3RD 4TH 5TH 5TH 5TH 5TH 5TH 5TH 5TH 5TH 5TH SUBCATEGORY/DEPT ---------------------------ROWS IN TABLE SALARY > $20K NAME LIKE ABC% NUMBER MALES ADMINISTRATION SYSTEMS DEVELOPMENT CENTER INFORMATION CENTER MANUFACTURING SYSTEMS OPERATIONS PLANNING SOFTWARE SUPPORT SPIFFY COMPUTER SERVICE DIV. SUPPORT SERVICES #ROWS ----32 25 0 19 6 0 3 9 5 1 4 3 1
318
DB2 9 Cookbook
319
DB2 9 Cookbook
320
DB2 9 Cookbook
321
DB2 9 Cookbook
CREATE FUNCTION reverse(instr VARCHAR(50)) RETURNS VARCHAR(50) BEGIN ATOMIC DECLARE outstr VARCHAR(50) DEFAULT ''; DECLARE curbyte SMALLINT DEFAULT 0; SET curbyte = LENGTH(RTRIM(instr)); WHILE curbyte >= 1 DO SET outstr = outstr || SUBSTR(instr,curbyte,1); SET curbyte = curbyte - 1; END WHILE; RETURN outstr; END! ANSWER SELECT id AS ID ==================== ,name AS NAME1 ID NAME1 NAME2 ,reverse(name) AS NAME2 -- -------- ------FROM staff 10 Sanders srednaS WHERE id < 40 20 Pernal lanreP ORDER BY id! 30 Marenghi ihgneraM
Notes
User-defined function scans input, one byte at a time, from back to front.
322
DB2 9 Cookbook
Quirks in SQL
Quirks in SQL
323
DB2 9 Cookbook
Timestamp Comparison
Incorrect
WITH TEMP1 (c1,T1,T2) AS (VALUES ('A' ,TIMESTAMP('1996-05-01-24.00.00.000000') ,TIMESTAMP('1996-05-02-00.00.00.000000') )) SELECT c1 FROM TEMP1 WHERE T1 = T2; ANSWER ========= <no rows>
Correct
WITH TEMP1 (c1,T1,T2) AS (VALUES ('A' ,TIMESTAMP('1996-05-01-24.00.00.000000') ,TIMESTAMP('1996-05-02-00.00.00.000000') )) SELECT c1 FROM TEMP1 WHERE T1 + 0 MICROSECOND = T2 + 0 MICROSECOND; ANSWER ====== C1 -A
Quirks in SQL
324
DB2 9 Cookbook
Notes
If no column function, row returned on if one matches If column function (and no HAVING or GROUP BY), always get row If column function and HAVING, row returned only if HAVING predicate true If column function and GROUP BY row returned on if one matches
Quirks in SQL
325
DB2 9 Cookbook
ANSWER ====== 0
Notes
If no column function, row returned on if one matches If column function (and no HAVING or GROUP BY), always get row If column function and HAVING, row returned only if HAVING predicate true If column function and GROUP BY row returned on if one matches
Quirks in SQL
326
DB2 9 Cookbook
SQL Design
Create DUMMY table with one row Left outer join to DATA table COALESCE function returns data if rows found, else DUMMY contents
Alternative Design
Use WITH to define one row table - instead of SYSDUMMY1
Quirks in SQL
327
DB2 9 Cookbook
Explanation
WITH TEMP AS (SELECT ID ,NAME ,(INT(RAND(0)* 10) * 10) + 10 AS RAN FROM STAFF WHERE ID <= 100 ) SELECT T.* ,CASE ID WHEN RAN THEN 'Y' ELSE ' ' END AS EQL FROM TEMP T ORDER BY ID; ANSWER ==================== ID NAME RAN EQL --- -------- --- --10 Sanders 10 Y 20 Pernal 30 30 Marenghi 70 40 O'Brien 10 50 Hanes 30 60 Quigley 40 70 Rothman 30 80 James 100 90 Koonitz 40 100 Plotz 100 Y
Quirks in SQL
328
DB2 9 Cookbook
Quirks in SQL
329
DB2 9 Cookbook
Degrees of Randomness
Random number of rows returned Fixed number of random rows, but possibly the same row returned twice Fixed number of random rows, each row distinct
Quirks in SQL
330
DB2 9 Cookbook
Date/Time Manipulation (1 of 2)
Wrong
WITH TEMP1 (BGN_TSTAMP, ELP_SEC) AS (VALUES (TIMESTAMP('2001-01-15-01.02.03.000000'), 1.234) ,(TIMESTAMP('2001-01-15-01.02.03.123456'), 1.234) ) SELECT BGN_TSTAMP ,ELP_SEC ,BGN_TSTAMP + ELP_SEC SECONDS AS END_TSTAMP FROM TEMP1; ANSWER ====== BGN_TSTAMP -------------------------2001-01-15-01.02.03.000000 2001-01-15-01.02.03.123456
Notes
DB2 ignores fractional values in date/time calculations To get correct answer, integer value of small time unit
Quirks in SQL
331
DB2 9 Cookbook
Date/Time Manipulation (2 of 2)
Right
WITH TEMP1 (BGN_TSTAMP, ELP_SEC) AS (VALUES (TIMESTAMP('2001-01-15-01.02.03.000000'), 1.234) ,(TIMESTAMP('2001-01-15-01.02.03.123456'), 1.234) ) SELECT BGN_TSTAMP ,ELP_SEC ,BGN_TSTAMP + (ELP_SEC *1E6) MICROSECONDS AS END_TSTAMP FROM TEMP1; ANSWER ====== BGN_TSTAMP -------------------------2001-01-15-01.02.03.000000 2001-01-15-01.02.03.123456
Notes
Previous query Above query Used "n.nnn" seconds Uses "nnnn" microseconds
Quirks in SQL
332
DB2 9 Cookbook
On VARCHAR Field
WITH TEMP1 (C0,c1,V1) AS (VALUES ('A',CHAR(' ',1),VARCHAR(' ',1)), ('B',CHAR(' ',1),VARCHAR('' ,1))) SELECT C0 FROM TEMP1 WHERE c1 = V1 AND V1 LIKE ' '; ANSWER ====== C0 -A
ANSWER ====== C0 -A B
333
DB2 9 Cookbook
Comparing Weeks
Compare week 33 over 10 years
WITH TEMP1 (YYMMDD) AS (VALUES DATE('2000-01-01') UNION ALL SELECT YYMMDD + 1 DAY FROM TEMP1 WHERE YYMMDD < '2010-12-31' ) SELECT YY AS YEAR ,CHAR(MIN(YYMMDD),ISO) AS MIN_DT ,CHAR(MAX(YYMMDD),ISO) AS MAX_DT FROM (SELECT YYMMDD ,YEAR(YYMMDD) YY ,WEEK(YYMMDD) WK FROM TEMP1 WHERE WEEK(YYMMDD) = 33 )AS XXX GROUP BY YY ,WK; ANSWER ========================== YEAR MIN_DT MAX_DT ---- ---------- ---------2000 2000-08-06 2000-08-12 2001 2001-08-12 2001-08-18 2002 2002-08-11 2002-08-17 2003 2003-08-10 2003-08-16 2004 2004-08-08 2004-08-14 2005 2005-08-07 2005-08-13 2006 2006-08-13 2006-08-19 2007 2007-08-12 2007-08-18 2008 2008-08-10 2008-08-16 2009 2009-08-09 2009-08-15 2010 2010-08-08 2010-08-14
Quirks in SQL
334
DB2 9 Cookbook
Rounding
SELECT FROM SUM(INTEGER(ROUND(SALARY,-1))) AS S1 ,INTEGER(SUM(SALARY)) AS S2 STAFF; ANSWER ============= S1 S2 ------ -----583640 583647
Quirks in SQL
335
DB2 9 Cookbook
Correct
SELECT LASTNAME ,SEX ,CASE WHEN SEX >= 'M' THEN 'MAL' WHEN SEX >= 'F' THEN 'FEM' END AS SXX FROM EMPLOYEE WHERE LASTNAME LIKE 'J%' ORDER BY 1; ANSWER ================= LASTNAME SX SXX ---------- -- --JEFFERSON M MAL JOHNSON F FEM JONES M MAL
Quirks in SQL
336
DB2 9 Cookbook
1st Field
Does division AFTER average Is probably what the user wants
2nd Field
Does division BEFORE average Does NOT weight rows for absolute size of values Is probably incorrect
Quirks in SQL
337
DB2 9 Cookbook
Note
To get rows in DATE sequence, order on the DATE field Do NOT order on CHARACTER equivalent of DATE field
Quirks in SQL
338
DB2 9 Cookbook
Quirks in SQL
339
DB2 9 Cookbook
HEX Representation
WITH TEMP (F1,F2) AS (VALUES (FLOAT(1.23456789E1 * 10 * 10 * 10 * 10 * 10 * 10 * 10) ,FLOAT(1.23456789E8))) SELECT HEX(F1) AS HEX_F1 ,HEX(F2) AS HEX_F2 FROM TEMP ANSWER WHERE F1 <> F2; ================================= HEX_F1 HEX_F2 ---------------- ---------------FFFFFF53346F9D41 00000054346F9D41
Quirks in SQL
340
DB2 9 Cookbook
ANSWER (1 row returned) ============================== DEC1 = 1.0 DEC2 = 0.33333333333333333333 DBL1 = +1.00000000000000E+000 DBL2 = +3.33333333333333E-001
Quirks in SQL
341
DB2 9 Cookbook
Quirks in SQL
342
DB2 9 Cookbook
Quirks in SQL
343