It actually has to do with the fact that you have column names that are the same in both tables. So in your sub-query, where you have:
WHERE B.SGBSTDN_PIDM = SGBSTDN_PIDM
It actually is looking in table B for both values instead of matching table A with table B.
On Thu, Apr 7, 2016 at 1:39 PM, jollydoe bounce-jollydoe@toadworld.com wrote:
Subquery alias and same tables?
Thread created by jollydoe
Greetings!
I am not able to figure out why the code below gives me 5543 rows of data whereas (please go to the sentence after the SQL statement)
...........................................................................................................................................................................................................................................................................................................
SELECT SPRIDEN_ID StudentID, (SPRIDEN_FIRST_NAME || SPRIDEN_LAST_NAME) FullName, SPRHOLD_HLDD_CODE Code, STVHLDD_DESC Description
FROM SPRHOLD, STVHLDD, SPRIDEN, SGBSTDN A
WHERE SPRHOLD_TO_DATE >= SYSDATE
AND SPRHOLD_HLDD_CODE = STVHLDD_CODE
AND SPRHOLD_PIDM = SPRIDEN_PIDM
AND SPRHOLD_PIDM = SGBSTDN_PIDM
AND SPRIDEN_CHANGE_IND IS NULL
AND SGBSTDN_PRIM_ROLL_IND = 'Y'
AND SGBSTDN_STST_CODE in ('AS','OW','AW','RA', 'DW','PA','SP','CW','WN','DA','DP')
AND SGBSTDN_TERM_CODE_EFF = (SELECT MAX(SGBSTDN_TERM_CODE_EFF)
FROM SGBSTDN B
WHERE B.SGBSTDN_PIDM = A.SGBSTDN_PIDM
AND SGBSTDN_TERM_CODE_EFF <= 201520
)
;
...........................................................................................................................................................................................................................................................................................................
Removing the alias from the parent select statement reduces rows of data to 2809. Specifically, the modified SQL looks like this: (Note: Adding an alias to both same table (SGBSTDN) gives me the same result as query result from query above i.e. 5543 rows.)
...........................................................................................................................................................................................................................................................................................................
SELECT SPRIDEN_ID StudentID, (SPRIDEN_FIRST_NAME || SPRIDEN_LAST_NAME) FullName, SPRHOLD_HLDD_CODE Code, STVHLDD_DESC Description
FROM SPRHOLD, STVHLDD, SPRIDEN, SGBSTDN
WHERE SPRHOLD_TO_DATE >= SYSDATE
AND SPRHOLD_HLDD_CODE = STVHLDD_CODE
AND SPRHOLD_PIDM = SPRIDEN_PIDM
AND SPRHOLD_PIDM = SGBSTDN_PIDM
AND SPRIDEN_CHANGE_IND IS NULL
AND SGBSTDN_PRIM_ROLL_IND = 'Y'
AND SGBSTDN_STST_CODE in ('AS','OW','AW','RA', 'DW','PA','SP','CW','WN','DA','DP')
AND SGBSTDN_TERM_CODE_EFF = (SELECT MAX(SGBSTDN_TERM_CODE_EFF)
FROM SGBSTDN B
WHERE B.SGBSTDN_PIDM = SGBSTDN_PIDM
AND SGBSTDN_TERM_CODE_EFF <= 201520
)
;
...........................................................................................................................................................................................................................................................................................................
I am guessing it has something to do with parent child table relationship but I can't put my head around it. Any explanation would be appreciated.
To reply, please reply-all to this email.
Stop receiving emails on this subject.
Or Unsubscribe from Toad for Oracle - General notifications altogether.
Toad for Oracle - Discussion Forum
Flag this post as spam/abuse.
--
Phyllis Helton
Data Magician
Digital Products & Strategies, Cru | Data Sciences & Analytics
Office
407-515-4452
phyllis.helton@cru.org
