Professional Documents
Culture Documents
2/13/2008 Purpose This document will illustrate how outer joins work and discusses how to ensure filters work as intended with outer joins. Here are the two example tables used throughout this document: V_AC_APPL APPL_KEY EDW_PERS_ID TERM_CD APPL_NBR 101 12 420048 1 301 13 420058 1 401 13 420068 2 501 14 420078 1 601 15 420088 1 V_AC_APPL_ATTR APPL_KEY ATTR_CD 101 A 301 B Examples
INNER JOIN
Problem: Find applications with student attributes Solution: Select APPL.APPL_KEY,APPL.EDW_PERS_ID,APPL.TERM_CD, APPL.APPL_NBR, ATTR.ATTR_CD,ATTR.AC_DT From V_AC_APPL APPL, V_AC_APPL_ATTR ATTR Where APPL.APPL_KEY=ATTR.APPL_KEY Result: ROW APPL_KEY EDW_PERS_ID TERM_CD APPL_NBR ATTR_CD AC_DT 1 101 12 420048 1 A 9/12/2005 2 301 13 420058 1 B 9/12/2007 NOTE: The result set contains the selected columns from those rows on both tables whose keys matched
1 of 5
OUTER JOINS
When to Use an Outer Join When you need join two tables that do not always have the matching keys, you will need to use an outer join if you need to pull all rows from the dominant table. Technically the relationship between the two table would be called 1 to 0-to-Many rows. In the illustration below V_AC_APPL would be the table referred to as 1 and V_AC_APPL_ATTR is the 0-toMany rows table.
Outer Join Syntax as Used in Business Objects: Problem: Find all applications and list the students attributes of the applications that have them Solution: Select APPL.APPL_KEY,APPL.EDW_PERS_ID,APPL.TERM_CD, APPL.APPL_NBR, ATTR.ATTR_CD,ATTR.AC_DT From V_AC_APPL APPL, V_AC_APPL_ATTR ATTR Where APPL.APPL_KEY=ATTR.APPL_KEY(+) NOTE: V_AC_APPL is the dominant table, i.e. the table for which all its rows (which satisfy any additional filters) will be in the result set. V_AC_APPL_ATTR is the subordinate table, i.e., the table which will contribute rows to the result set when a match to the dominant table exists. Alternate Syntax: ANSI Standard Out Join Oracle9i supports the ANSI/ISO SQL: 1999 standards. There are three variations on the outer join. The LEFT OUTER JOIN returns all the rows from the table on the left side of the join, along with the values from the right hand side, or NULLs if a matching row doesn't exist. The RIGHT OUTER JOIN does the reverse of this. Finally, the FULL OUTER JOIN returns all rows from both tables, filling in any blanks with NULLs: Heres the solution immediately above rewritten as a Left Outer Join: Solution: Select Select APPL.APPL_KEY,APPL.EDW_PERS_ID,APPL.TERM_CD, APPL.APPL_NBR, ATTR.ATTR_CD,ATTR.AC_DT From V_AC_APPL APPL LEFT OUTER JOIN V_AC_APPL_ATTR ATTR ON (APPL.APPL_KEY=ATTR.APPL_KEY)
2 of 5
Left Outer Join Result: Row APPL_KEY EDW_PERS_ID 1 101 12 2 301 13 3 401 13 4 501 14 5 601 15
APPL_NBR 1 1 2 1 1
NOTE: Observe last two columns contain NULLs. Since the table V_AC_APPL_ATTR did not have any rows whose appl_key values were either 501 or 601, the results for the columns from V_AC_APPL_ATTR will be NULL. BEST PRACTICE TIP If you want to further constrain on a subordinate table column, be sure your SQL addresses the possible NULL values in the result set for filtering subordinate table column(s). Using the NULL value function appropriately will prevent the undesirable results that may arise when NULL values are possible. In the case of the last example, this tip would refer to the possible NULL values that columns ATTR_CD or AC_DT in the result set if the query also included filters on either or both columns. Heres when youll need the NULL Value function: Your query includes one or more outer joins. The more outer joins in the query, higher the risk of undesirable results. Your query also filters on a subordinate table column that is NOT a key column used in the join itself.
3 of 5
ANSI Standard Left Outer Join Problem: (Same as immediately above.) Solution: Select APPL_KEY,EDW_PERS_ID,TERM_CD,APPL_NBR,ATTR_CD, AC_DT From V_AC_APPL APPL LEFT OUTER JOIN V_AC_APPL_ATTR ATTR ON (APPL.APPL_KEY=ATTR.APPL_KEY) Where NVL(ATTR.AC_DT,to_date(9/12/2005, MM/DD/YYYY)) <=to_date(9/12/2005, MM/DD/YYYY) How The Solutions Work NVL(x,y) is the NULL value function. This function checks to see if x is NULL. If x is NULL, then the value of y is substituted. To_date(x,y) is the function that converts the character string in x to a datetime value in the date format y. The examples use the to_date function in order to compare an explicit character string to the
Results for the Outer Join with a Filter Row 1 3 4 5 APPL_KEY 101 401 501 601 EDW_PERS_ID 12 13 14 15 TERM_CD 420048 420068 420078 420088 APPL_NBR 1 2 1 1 ATTR_CD A NULL NULL NULL AC_DT 9/12/2005 NULL NULL NULL
How the Filter evaluated NVL(ATTR.AC_DT, to_date(9/12/2007, MM/DD/YYYY))<=to_date(9/12/2007, MM/DD/YYYY) Row 1 9/12/2005 <= 9/12/2005 -- True (include in results) 2 9/12/2007 <=9/12/2005 FALSE (omit from results) 3 (NULL replaced) 9/12/2005 <=9/12/2005 True (include in results) 4 (NULL replaced) 9/12/2005 <=9/12/2005 True (include in results) 5 (NULL replaced) 9/12/2005 <=9/12/2005 True (include in results) Using Current Syntax constraining on ATTR_CD: Problem: Find all applications and list the student attributes if the Attribute Code = A. Select APPL_KEY,EDW_PERS_ID,TERM_CD,APPL_NBR,ATTR_CD, AC_DT From V_AC_APPL APPL, V_AC_APPL_ATTR ATTR Where APPL.APPL_KEY=ATTR.APPL_KEY(+) And NVL(ATTR_CD, A) =A How Filter evaluated Row ATTR_CD 1 A 2 B 3 NULL 4 NULL 5 NULL
NVL(ATTR_CD, A) =A ATTR_CD =A -- True ATTR_CD = A -- False ATTR_CD is NULL, but set to A which is = A -- True ATTR_CD is NULL, but set to A which is = A -- True ATTR_CD is NULL, but set to A which is = A -- True
4 of 5
Result data set (Row 2 is filtered out which is correct) Row 1 3 4 5 APPL_KEY 101 401 501 601 EDW_PERS_ID 12 13 14 15 TERM_CD 420048 420068 420078 420088 APPL_NBR 1 2 1 1 ATTR_CD A NULL NULL NULL AC_DT 9/12/2005 NULL NULL NULL
5 of 5