Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki...

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

Transcript of Correlated Subqueries Chapter 2 Supplement 1 © Spring 2012 Imelda Go, John Grego, Jennifer Lasecki...

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

Correlated Subqueries

Chapter 2 Supplement

1© Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South

Carolina

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

Example – Correlated Subquery

Player Atbats

Walker 271

Wingo 240

Thomas 231

Marzilli 220

Beary 211

Morales 249

Mooney 254

Williams 209

Bradley Jr.

162

2

Player Position

Marzilli Outfield

Williams Outfield

Bradley Jr. Outfield

Wingo Infield

Walker Infield

Thomas Infield

Marzilli Infield

Morales Infield

Mooney Infield

AtBatsPlayerposition

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

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.player); quit;

3

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

Example Correlated Subquery

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 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

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

Example – Correlated Subquery

Player Atbats

Walker 271

Wingo 240

Thomas 231

Beary 211

Morales 249

Mooney 254

5

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

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

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

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