33
1

Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

1

Page 2: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

2

Page 3: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 3

Page 4: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Tuning SQL – Sheryl M. Larsen, Inc. 4

Page 5: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

IBM Confidential 5

This chart shows the relationship of DB2 for Linux, Unix & Windows with DB2 for z/OS. This step in the process is DB2 10 for

There are three sets of SQL noted above, with some that is unique to DB2 for z/OS in the first group, SQL that is common acro

The Cross-Platform SQL Reference Version 4.1 documents the prior combination, with DB2 for i V7.1.Cross-Platform Development Version 4.1, http://www.ibm.com/developerworks/db2/library/techarticle/0206sqlref/0206sqlref.html

Page 6: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 6

Page 7: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 7

Page 8: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 8

Page 9: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 9

Page 10: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 10

Page 11: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 11

The big difference between UNIONs and EXCEPT/INTERSECT is the ability to

use correlation between query blocks. UNIONs are completely independent.

Page 12: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

This technique works when the lists compared and the EXCEPT operation are

easily arrived.

SMLI.2009 12

Page 13: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

One more sample.

SMLI.2009 13

Page 14: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 14

Page 15: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 15

Window-order-by clauses are used with RANK, DENSE_RANK and

ROW_NUMBER functions.

RANK is a mixture of DENSE_RANK and ROW_NUMBER.

Page 16: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 16

This DB2 9 feature is very powerful . It provides sequencing within any query

block. Nesting and ORDER BY and FETCH FIRST allows for more complex

ranking.

Page 17: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 17

Page 18: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 18

Page 19: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 19

Typical summary reports also require detail data from another table.

GROUP BY prohibits getting the summary data and detail together

Table Expressions can be used to separate the GROUP BY work which is

usually done against one table.

The first benefit is the number of rows participating in the join are now

reduced from the GROUP BY processing.

The second benefit occurs when an index is available on the GROUP BY

column. This enables the DB2 to process the groups one at a time and pass

them into the join without needing a sort.

DB2 9 allows FETCH FIRST and ORDER BY inside table expressions and

other query blocks. This adds powerful ranking capabilities.

Page 20: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 20

DB2 9 allows FETCH FIRST and ORDER BY inside table expressions and

other query blocks. This adds powerful ranking capabilities.

Each queryblock can independently sequence data prior to final ORDER BY.

Page 21: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

Advanced SQL – Sheryl M. Larsen, Inc. 21

Page 22: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

This is IBM’s proposed SQL text from DB2X and is subject to change.

Moving averages are calculated over a defined range using PRECEDING and

FOLLOWING key words.

Advanced SQL – Sheryl M. Larsen, Inc. 22

Page 23: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

SMLI.2009 23

Page 24: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

More information on this syntax can be found in the Database Fundamentals :

http://publib.boulder.ibm.com/infocenter/db2luw/v9r5/index.jsp

Advanced SQL – Sheryl M. Larsen, Inc. 24

Page 25: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

SMLI.2009 25

Page 26: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

MIA = Multiple index access

NR = New Range

SMLI.2009 26

Page 27: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

27

Page 28: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

28

Page 29: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

29

Page 30: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

30

Page 31: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

31

Page 32: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

32

Page 33: Advanced SQL - DBI SoftwareAdvanced SQL – Sheryl M. Larsen, Inc. 19 Typical summary reports also require detail data from another table. GROUP BY prohibits getting the summary data

33