Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,69 MB
Nội dung
220 10.3 Examining Different Types of Joins Refer back to Figure 10.15 to validate artists who do not have guest appearances on any songs. You will see that these artists (starting with Sheryl Crow and ending with James Taylor) appear in Figure 10.17 with a blank space in the SONG_ID and GUESTARTIST_ID. The query could not match any row in the GUESTAPPEARANCE table with these artists in the ARTIST table. Oracle Database 10g automatically returns a null value as a placeholder in the results for the unmatched rows. Look at the last five rows in Figure 10.17. These are the artists who do make guest appearances. Notice that the ARTIST_ID column and the GUESTARTIST_ID column contain the same number in every row. This makes sense because the query equates the values in the two columns. These rows are finding themselves in the ARTIST table. Any row in the GUE- STAPPEARANCE table must match a row in the ARTIST table. The second left outer join query, shown following, is the ANSI version of the first left outer join query. The result is shown in Figure 10.18. One difference between the Oracle format join in Figure 10.17 and the ANSI format join in Figure 10.18 is the sorted order of null values. SELECT A.NAME, GA.SONG_ID, A.ARTIST_ID, GA.GUESTARTIST_ID FROM ARTIST A LEFT OUTER JOIN GUESTAPPEARANCE GA ON (A.ARTIST_ID = GA.GUESTARTIST_ID); Figure 10.17 Oracle Format Left Outer Join of ARTIST and GUESTAPPEARA NCE Tables. Chap10.fm Page 220 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 10.3 Examining Different Types of Joins 221 Chapter 10 The third and last left outer join query is a more complex variation of the first two using the ANSI format and the DECODE function. Note: The DECODE function is an embedded case statement (see Chapter 9). The following query lists all of the artists in the ARTIST table. It returns one of two phrases, depending on whether the artist makes a guest appearance on a song or not. If not, the phrase “ is an Artist.” follows the artist’s name. If otherwise, the phrase “ made a guest appearance on …” fol- lows the artist’s name, including the appropriate song title. The result as shown in Figure 10.19 is a left outer join between all three ARTIST, GUE- STAPPEARANCE, and SONG tables. SELECT A.NAME|| DECODE (S.TITLE, NULL,' is an Artist.' ,' made a guest appearance on '||S.TITLE||'.' ) as "What they did" FROM ARTIST A LEFT OUTER JOIN GUESTAPPEARANCE GA ON (A.ARTIST_ID = GA.GUESTARTIST_ID) LEFT OUTER JOIN SONG S ON (S.SONG_ID = GA.SONG_ID) ORDER BY A.NAME, S.TITLE; Figure 10.18 ANSI Format Left Outer Join of the ARTIST and GUESTAPPEARA NCE Tables. Chap10.fm Page 221 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 222 10.3 Examining Different Types of Joins Notice in the Oracle-formatted query in Figure 10.19 that the two left outer joins are identified by the (+) symbol next to the appropriate columns in the WHERE clause. Here is another variation that returns the same result. In the following query, the Oracle format uses an embedded subquery statement (see Chap- ter 12) rather than a WHERE clause addition using the SONG and GUE- STAPPEARANCE tables. SQL is very versatile. There are many options available in SQL. Figure 10.19 Left Outer Join Between ARTIST, GUESTAPPEAR ANCE, and SONG Tables. Chap10.fm Page 222 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 10.3 Examining Different Types of Joins 223 Chapter 10 SELECT A.NAME|| DECODE(NVL( (SELECT TITLE FROM SONG WHERE SONG_ID = GA.SONG_ID) , NULL), NULL,' is an Artist.' ,' made a guest appearance on ' ||NVL((SELECT TITLE FROM SONG WHERE SONG_ID = GA.SONG_ID),NULL)||'.' ) AS "What they did" FROM ARTIST A, GUESTAPPEARANCE GA WHERE A.ARTIST_ID = GA.GUESTARTIST_ID(+) ORDER BY A.NAME, GA.SONG_ID; 10.3.3.2 Right Outer Join A right outer join is the converse of a left outer join. A right outer join returns all rows from the table on the right of the join plus any matching rows from the table on the left. Rows from the table on the right with no matching rows in the table on the left will contain null values for the col- umns from the table on the left side. Following is an example of an ANSI-formatted right outer join state- ment. The equivalent Oracle form with an outer join on three tables does not exist unless a subquery is used (see Chapter 12). It is not possible to execute an outer join between more than two tables in a single query using the Oracle format; an error will result (ORA-01417: a table may be outer joined to at most one other table). The result of the following query is shown in Figure 10.20. The query in Figure 10.20 is an ANSI format right outer join between all three ARTIST, GUESTAPPEARANCE, and SONG tables. SELECT A.NAME "Artist", S.TITLE "Song" FROM GUESTAPPEARANCE GA RIGHT OUTER JOIN SONG S ON (GA.SONG_ID = S.SONG_ID) RIGHT OUTER JOIN ARTIST A ON (GA.GUESTARTIST_ID = A.ARTIST_ID) ORDER BY S.TITLE, A.NAME; The query first performs a right outer join between the GUESTAP- PEARANCE and SONG tables. Because the SONG table is on the right, all songs are retrieved. Next, this result set is right outer joined to the ART- IST table using the GUESTARTIST_ID. Because not all songs have a guest appearance, those songs have null values in the GUESTARTIST_ID and Chap10.fm Page 223 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 224 10.3 Examining Different Types of Joins therefore are not able to match with a row in the ARTIST table. Because the ARTIST table is now on the right, the final result returns all artists and only the songs having a guest appearance. The song “Stop” is listed three times because three artists played as guests on “Stop”: Angie Aparo, Paul Doucette, and Tony Adams. 10.3.3.3 Full Outer Join A full outer join will return all rows in both tables, filling in missing values with null values when a row is not present on the other side of the join. Note: There is no Oracle format equivalent for a full outer join. The next query is an ANSI standard format, full outer join between the ARTIST, GUESTAPPEARANCE, and SONG tables. The result is shown in Figure 10.21. Figure 10.20 A Right Outer Join Between ARTIST, GUESTARTIST, and SONG Tables. Chap10.fm Page 224 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 10.3 Examining Different Types of Joins 225 Chapter 10 COLUMN NAME FORMAT A32 HEADING "Artist" COLUMN TITLE FORMAT A32 HEADING "Song" SELECT A.NAME AS NAME, S.TITLE AS TITLE FROM ARTIST A FULL OUTER JOIN GUESTAPPEARANCE GA ON (A.ARTIST_ID = GA.GUESTARTIST_ID) FULL OUTER JOIN SONG S ON (GA.SONG_ID = S.SONG_ID) ORDER BY NAME, TITLE; The query lists all artists and all songs, matching songs and artists together if the artist makes a guest appearance on the related song. If an art- ist does not make a guest appearance, the song title is NULL (outer join between artists and guest appearances). If a song has no guest appearances, the artist name is NULL (outer join between songs and guest appearances). Figure 10.21 shows only part of the results, illustrating how either the title or the name can be NULL. There are 130 rows returned in the query. 10.3.4 Self-Join A self-join joins a table to itself. Table aliases must be used to distinguish between two different copies of the same table. A table such as this would Figure 10.21 A Full Outer Join Between ARTIST, GUESTAPPEARA NCE, and SONG Tables. Chap10.fm Page 225 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 226 10.3 Examining Different Types of Joins be a candidate for further Normalization or is a result of a Denormaliza- tion performance improvement. Some examples of situations in which self-joins might be useful would be grouping self-joins or hierarchical (fishhook) self-joins. Note: A fishhook is a table with a one-to-many relationship to its own pri- mary key. Thus the primary key would be both primary key and a unique foreign key. 10.3.4.1 Grouping Self-Join A grouping self-join implies that some rows have a one-to-many relation- ship with other rows in the same table. There is a “Best of” compilation CD by Sheryl Crow in the MUSIC schema containing songs on other CDs. The self-join query following lists SONG_ID values appearing on more than one CD. Note that the line in the WHERE clause containing the ine- quality operator will prevent any song from being listed twice. The result is shown in Figure 10.22. SELECT B.MUSICCD_ID, B.TRACK_SEQ_NO, A.SONG_ID FROM CDTRACK A JOIN CDTRACK B ON (A.SONG_ID = B.SONG_ID) WHERE B.MUSICCD_ID <> A.MUSICCD_ID ORDER BY MUSICCD_ID, TRACK_SEQ_NO, SONG_ID; This self-join searches for tracks (songs) that are found on more than one CD. Picture in your mind’s eye two copies of the CDTRACK table side by side. Each row in the left table (Table A) is matched with one row (itself) or more than one row (same song on another CD) in the right table (Table B). Eliminate the rows where you have matched a track to itself by comparing the MUSICCD_ID in the two rows. If the SONG_ID values are the same but the MUSICCD_ID values are different, the song is selected in the query. The SONG_ID value 1 in Figure 10.22 appears on two CDs: #1 and #11. The next query contains all tracks by Sheryl Crow; the inequality opera- tor is now missing. The result is shown in Figure 10.23. SET PAGES 80 LINESIZE 132 COLUMN CD FORMAT A24 HEADING "CD" COLUMN TRACK FORMAT 990 HEADING "Track" Chap10.fm Page 226 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 10.3 Examining Different Types of Joins 227 Chapter 10 COLUMN SONG FORMAT A36 HEADING "Song" SELECT CD.TITLE AS CD, T.TRACK_SEQ_NO AS TRACK , S.TITLE AS SONG FROM SONG S, CDTRACK T, MUSICCD CD, ARTIST A WHERE A.NAME = 'Sheryl Crow' AND A.ARTIST_ID = S.ARTIST_ID AND S.SONG_ID = T.SONG_ID AND T.MUSICCD_ID = CD.MUSICCD_ID ORDER BY CD, SONG; Including the CD and song titles in Figure 10.23 makes it easier to see how the query works. The CD called “The Best of Sheryl Crow” has six songs. Two of the songs are from the “Soak Up the Sun” CD and four of the songs are from the “C’mon, C’mon” CD. Figure 10.22 A Barebones Self- Join on the CDTRACK Table. Chap10.fm Page 227 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 228 10.3 Examining Different Types of Joins 10.3.4.2 Hierarchical (Fishhook) Self-Join A hierarchical or fishhook self-join is a tree-like structure where parent rows have child rows, which can in turn be parent rows of other child rows. A common use for this type of join is to represent family tree data. The MUSIC schema used in this book has two tables containing hierarchical structures: the INSTRUMENT and GENRE tables. Only the INSTRU- MENT table contains hierarchical data, in addition to just structure. SELECT PARENT.NAME "Parent", CHILD.NAME "Child" FROM INSTRUMENT PARENT JOIN INSTRUMENT CHILD Figure 10.23 Descriptive Form of the Self-Join in Figure 10.22. Chap10.fm Page 228 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. 10.3 Examining Different Types of Joins 229 Chapter 10 ON (PARENT.INSTRUMENT_ID = CHILD.SECTION_ID) ORDER BY PARENT.NAME, CHILD.NAME; Figure 10.24 contains the result of the query. Notice how the Alto Horn, Baritone Horn, and Clarinet are part of Woodwind instruments. Additionally, Woodwind instruments are part of Wind instruments. That is a three-layer hierarchical representation. Note: See Chapter 13 for details on hierarchical queries. Figure 10.24 A Hierarchical Data Fishhook Self-Join. Chap10.fm Page 229 Thursday, July 29, 2004 10:08 PM Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... for joining tables Joins can get much more complicated than those contained within this chapter However, some highly complex mutable joins can be simplified with the use of subqueries Chapter 12 examines subqueries The next chapter shows you how to summarize data using aggregate functions with the GROUP BY clause 10.4 Endnotes 1 Oracle Performance Tuning for 9i and 10g (ISBN: 1-55558-305-9) Chapter 10... is a little bit more of statistics than Oracle SQL for this book Chapter 11 Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark 240 11.2 Types of Group Functions 11.2.1.3 Statistical Distribution Functions CUME_DIST(expression [, expression ]) WITHIN GROUP (ORDER BY expression [, expression]) The cumulative distribution of an expression within a group of values A cumulative... above DENSE_RANK(expression [, expression ]) WITHIN GROUP (ORDER BY expression [, expression ]) The rank of a row within an ordered group of rows FIRST | LAST (expression [, expression ]) WITHIN GROUP (ORDER BY expression [, expression ]) The first and last ranking row in a sorted group of rows 11.2.1.5 Grouping Functions Grouping functions are used with analysis enhancements to define the sliding... HAVING Clause with Aggregate Functions WHERE (A.NAME BETWEEN 'A%' AND 'P%') GROUP BY A.NAME, A.EMAIL HAVING SUM(ST.AMOUNT_CHARGED) - SUM(ST.AMOUNT_PAID) > 4000; You can even use HAVING with aggregate functions not listed in the SELECT clause For example, change the HAVING clause in the query so that you are listing only artists with more than five visits The following query achieves this task, with its result... BY clause and its various additions, proceed onto grouping functions, and finish with the SPREADSHEET clause The SPREADSHEET clause is new to Oracle Database 10g 11.1 GROUP BY Clause Syntax In previous chapters you have explored the SELECT, FROM, WHERE, and ORDER BY clauses, plus methods of joining tables using both an Oracle proprietary join syntax and the ANSI JOIN clause syntax This chapter introduces... group functions behave with null values and the use of DISTINCT Let’s begin with null values 11.3.1 Group Functions and Null Values Most functions, except for COUNT(*) and GROUPING, when passed a null value, will return a NULL result COUNT(*) will always return one row Any other function returns a null value if no rows are found The NVL function can be used to replace a NULL with a value as shown in... point function is an inverse distribution function, we start with the probability and compute the corresponding value for the cumulative distribution 11.2.1.4 Ranking Functions RANK(expression [, expression ]) WITHIN GROUP (ORDER BY expression [, expression]) The rank of a value in a group of values PERCENT_RANK(expression [, expression ]) WITHIN GROUP (ORDER BY [, expression ]) A cumulative distribution... have to find at this stage is for the CDTRACK table SELECT COUNT(*) FROM CDTRACK; MUSICCD has 13 rows CDTRACK has 125 rows ARTIST has 15 rows SONG has 118 rows SONG_GUESTARTIST has 5 rows Let’s begin with an Oracle format query, the result of which is shown in Figure 10.25 This query returns 125 rows, equivalent to the largest table, validating this query as not being a Cartesian product COLUMN CD FORMAT... group of values A cumulative frequency distribution is a plot of the number of observations falling within or below an interval, a histogram The cumulative distribution function is the probability that a variable takes a value less than or equal to a given value PERCENTILE_{ CONT | DISC }(expression) WITHIN GROUP (ORDER BY expression) The percent point function or the inverse distribution function for... we group and sort with the GROUP BY clause? What are group functions? What are aggregate and analytic functions? What does the HAVING clause do? What do the ROLLUP, CUBE, and GROUPING SETS clauses do? What is the SPREADSHEET1 clause? This chapter shows you how to aggregate and summarize rows in queries based on specific columns and expressions, using the GROUP BY clause in conjunction with various types . You will see that these artists (starting with Sheryl Crow and ending with James Taylor) appear in Figure 10.17 with a blank space in the SONG_ID and GUESTARTIST_ID query could not match any row in the GUESTAPPEARANCE table with these artists in the ARTIST table. Oracle Database 10g automatically returns a null value as