在where子句中,我希望返回CRSE_NUMB末尾带有“E”的所有记录,或者返回ASCD_CODE中带有“AS”代码的所有记录。
如果SLRRASG表有一个外部连接,在where语句中添加这个OR条件是否正确?
select distinct
SPRIDEN.SPRIDEN_PIDM PIDM,
SFRSTCR.SFRSTCR_TERM_CODE Term_Code,
SFRSTCR.SFRSTCR_PTRM_CODE,
SPRIDEN.SPRIDEN_LAST_NAME LNAME,
SPRIDEN.SPRIDEN_FIRST_NAME FNAME,
SPRIDEN.SPRIDEN_ID SID,
SGBSTDN.SGBSTDN_COLL_CODE_1,
-- decode(SGBSTDN.SGBSTDN_COLL_CODE_1, 'YC', 'Yale_College', 'SU', 'Visiting_Student') Student_Type,
-- SFRSTCR.SFRSTCR_RSTS_CODE,
-- SGBSTDN.SGBSTDN_LEVL_CODE LVL_Code,
-- SFRSTCR.SFRSTCR_LEVL_CODE CRS_LVL,
SLRRASG.SLRRASG_ASCD_CODE Housing_Status,
-- SLRRASG.SLRRASG_BEGIN_DATE,
-- SLRRASG.SLRRASG_END_DATE,
SCBCRSE.SCBCRSE_SUBJ_CODE,
SCBCRSE.SCBCRSE_CRSE_NUMB,
SFRSTCR.SFRSTCR_CRN CRN,
-- SCBCRSE.SCBCRSE_TITLE,
-- SFRSTCR.SFRSTCR_ADD_DATE,
-- SCBCRSE.SCBCRSE_OTH_HR_LOW as "Credit",
-- SFRSTCR.SFRSTCR_BILL_HR,
-- SFRSTCR.SFRSTCR_CREDIT_HR
--SFRSTCR.SFRSTCR_CREDIT_HR,
listagg(distinct goremal.goremal_email_address, ',')
from
SATURN.SGBSTDN
join SATURN.SFRSTCR
on SGBSTDN.SGBSTDN_PIDM = SFRSTCR.SFRSTCR_PIDM
and SGBSTDN.SGBSTDN_TERM_CODE_EFF = SFRSTCR.SFRSTCR_TERM_CODE
join SATURN.SPRIDEN
on SGBSTDN.SGBSTDN_PIDM = SPRIDEN.SPRIDEN_PIDM
left join SATURN.SLRRASG
on SGBSTDN.SGBSTDN_PIDM = SLRRASG.SLRRASG_PIDM
and SGBSTDN.SGBSTDN_TERM_CODE_EFF = SLRRASG.SLRRASG_TERM_CODE
-- and SLRRASG.SLRRASG_ASCD_CODE = 'AS'
join SATURN.SSBSECT
on SFRSTCR.SFRSTCR_TERM_CODE = SSBSECT.SSBSECT_TERM_CODE
and SFRSTCR.SFRSTCR_CRN = SSBSECT.SSBSECT_CRN
join SATURN.SCBCRSE
on SSBSECT.SSBSECT_SUBJ_CODE = SCBCRSE.SCBCRSE_SUBJ_CODE
and SSBSECT.SSBSECT_CRSE_NUMB = SCBCRSE.SCBCRSE_CRSE_NUMB
left join GENERAL.GOREMAL
on SPRIDEN.SPRIDEN_PIDM = GOREMAL.GOREMAL_PIDM
and GOREMAL.GOREMAL_EMAL_CODE = 'HAPP'
where
SPRIDEN.SPRIDEN_ID like '9%'
and SFRSTCR.SFRSTCR_TERM_CODE = '202302'
and SGBSTDN.SGBSTDN_COLL_CODE_1 = 'SU'
and SPRIDEN.SPRIDEN_CHANGE_IND is null
and SFRSTCR.SFRSTCR_RSTS_CODE in ('RE', 'RW')
and SSBSECT.SSBSECT_SSTS_CODE in ('A', 'E', 'O')
and SFRSTCR.SFRSTCR_PTRM_CODE in ('H4B', 'H5A', 'H5B', 'H10', 'H8', 'HSL', 'HND', 'HE3', 'HEL', 'H6', 'HFY', 'HWY')
and (SCBCRSE.SCBCRSE_CRSE_NUMB not like '%E' or
SLRRASG.SLRRASG_ASCD_CODE = 'AS')
-- and SCBCRSE.SCBCRSE_CRSE_NUMB not like '%E'
and SCBCRSE.SCBCRSE_EFF_TERM = (
select max(scbcrse_eff_term) from saturn.scbcrse xppd
where
xppd.scbcrse_subj_code = scbcrse.scbcrse_subj_code
and
xppd.scbcrse_crse_numb = scbcrse.scbcrse_crse_numb and xppd.scbcrse_eff_term <= ssbsect_term_code)
group by
SPRIDEN.SPRIDEN_PIDM,
SFRSTCR.SFRSTCR_TERM_CODE,
SFRSTCR.SFRSTCR_PTRM_CODE,
SPRIDEN.SPRIDEN_LAST_NAME,
SPRIDEN.SPRIDEN_FIRST_NAME,
SPRIDEN.SPRIDEN_ID,
SGBSTDN.SGBSTDN_COLL_CODE_1,
SLRRASG.SLRRASG_ASCD_CODE,
SLRRASG.SLRRASG_BEGIN_DATE,
SLRRASG.SLRRASG_END_DATE,
SCBCRSE.SCBCRSE_SUBJ_CODE,
SCBCRSE.SCBCRSE_CRSE_NUMB,
SFRSTCR.SFRSTCR_CRN
2条答案
按热度按时间5fjcxozz1#
是的,这完全可以在
WHERE
子句中进行过滤。只要确保您包含一个条件,用于处理由于外部连接而导致的NULL值,如果您的逻辑需要它。您可能不需要它:如果SLRRASG_ASCD_CODE为NULL,则第二个过滤器将失败,但第一个过滤器仍可能成功,这可能是您想要的,在这种情况下,您可以。但是如果您想将NULL视为与'AS'相同,则应该添加:
m528fe3b2#
您希望将筛选器添加到联接条件:
如果您用途:
那么
t2
必须有一个匹配的行才能有一个匹配过滤器的ascd_code
,你的查询实际上会有一个INNER JOIN
而不是OUTER JOIN
,所以是一样的:如果你确实想在
WHERE
子句中进行过滤,那么你需要检查OUTER JOIN
中的行是否不匹配,你可以通过检查连接条件中使用的列是否为NULL
来做到这一点:如果你想在多个条件上过滤,并且如果外部连接匹配,它们都必须为true,那么:
或者,如果你想要:
t2.ascd_code = 'AS'
和t3.crse_numb
可以是任何值;或者t2.ascd_code
是任意值,并且t3.crse_numb
不以E
结尾,则:t2
或t3
始终匹配,无论哪个匹配的表都将有效地被INNER JOIN
ed,另一个表可以被OUTER JOIN
ed。