我有一个看起来像这样的查询:
SELECT 'FY2000' AS FY, COUNT(DISTINCT SGBSTDN_PIDM) AS CHEM_MAJORS
FROM SATURN.SGBSTDN, SATURN.SFRSTCR
WHERE SGBSTDN_PIDM = SFRSTCR_PIDM
AND SGBSTDN_TERM_CODE_EFF = (SELECT MAX(SGBSTDN_TERM_CODE_EFF)
FROM SATURN.SGBSTDN
WHERE SGBSTDN_TERM_CODE_EFF <= '200002'
AND SGBSTDN_PIDM = SFRSTCR_PIDM)
AND SGBSTDN_MAJR_CODE_1 = 'CHEM'
AND SFRSTCR_TERM_CODE BETWEEN '199905' AND '200002'
AND (SFRSTCR_RSTS_CODE LIKE 'R%' OR SFRSTCR_RSTS_CODE LIKE 'W%')
AND SFRSTCR_CREDIT_HR >= 1
它返回 48 的计数,我认为这是正确的。但是,我不明白为什么子查询不需要SATURN.SFRSTCR
在 FROM 子句中才能引用SFRSTCR_PIDM
. 我认为子查询是自包含的,看不到查询的其余部分?
但是,如果我将 SATURN.SFRSTCR 添加到子查询中,计数将更改为 22。如果我AND SGBSTDN_PIDM = SFRSTCR_PIDM
从子查询中取出,计数也会更改为 22。有人可以向我解释一下吗?