Presentation is loading. Please wait.

Presentation is loading. Please wait.

Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina.

Similar presentations


Presentation on theme: "Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina."— Presentation transcript:

1 Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

2 Example – Correlated Subquery PlayerAtbats Walker271 Wingo240 Thomas231 Marzilli220 Beary211 Morales249 Mooney254 Williams209 Bradley Jr.162 2 PlayerPosition MarzilliOutfield WilliamsOutfield Bradley Jr.Outfield WingoInfield WalkerInfield ThomasInfield MarzilliInfield MoralesInfield MooneyInfield AtBatsPlayerposition

3 Correlated Subqueries  In Chapter 2, we saw one example of a correlated subquery: proc sql; select player, atbats from atbats where "Infield"= (select position from playerposition where atbats.player=playerposition.playe r); quit; 3

4 Example Correlated Subquery Example Correlated Subquery Step 1 – The outer query takes the first row in atbats table and finds the columns player and atbats. Step 1 – The outer query takes the first row in atbats table and finds the columns player and atbats. Step 2 – Match atbats.player (passed from table in outer query) with playerposition.player to find the qualifying row in the playerposition table. Step 2 – Match atbats.player (passed from table in outer query) with playerposition.player to find the qualifying row in the playerposition table. Step 3 – The inner query now passes the position of the selected row in playerposition back to the outer query via the = operator, where the position is matched for the selection in the outer query. Step 3 – The inner query now passes the position of the selected row in playerposition back to the outer query via the = operator, where the position is matched for the selection in the outer query. 4

5 Example – Correlated Subquery PlayerAtbats Walker271 Wingo240 Thomas231 Beary211 Morales249 Mooney254 5

6 Example-Correlated Subquery  Correlated subqueries suggest that SAS has the ability to resolve ambiguous references.  Regardless, when creating code, we often get error messages that look like: ERROR: Unresolved reference to table/correlation name varname 6

7 Example-Correlated Subquery  We can modify the correlated subquery so that  The reference to playerposition.player is less clear  playerposition.player is not even selected in the outer query, but does appear in the referenced data set (atbats) 7


Download ppt "Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina."

Similar presentations


Ads by Google