简体   繁体   中英

Oracle SQL query with multiple columns using same subquery with OR

Need help in simplifying the below Oracle SQL query with OR condition: is it possible to using one single select query instead of multiple.

for this sub query "select usr_key from usr where usr_login='abcd'" i was able to make it into single instead of writing 4 times as it is just giving one value. but for the second sub query it is writing multiple values so i am not able to combine them.

select * from catalog  where 
(
(select usr_key from usr where usr_login='abcd') in (approver_user,CERTIFIER_USER,FULFILLMENT_USER) 
or ('abcd') in (EMERGENCYAPPROVERUSER,SPECIALAPPROVERUSER)
or approver_role IN 
(select ugp.ugp_key from ugp , usg,usr uuu where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd')
or CERTIFIER_ROLE IN 
(select ugp.ugp_key from ugp , usg,usr uuu where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd')
or FULFILLMENT_ROLE IN 
(select ugp.ugp_key from ugp , usg,usr uuu where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd')
)
and entity_type='Entitlement';


Use WITH clause

WITH some_query AS(
    select ugp.ugp_key from ugp , usg,usr uuu 
    where ugp.ugp_key=usg.ugp_key and usg.usr_key=uuu.usr_key  and uuu.usr_login='abcd'
)
select * from catalog  
where 
(
    (select usr_key from usr where usr_login='abcd') in (approver_user,CERTIFIER_USER,FULFILLMENT_USER) 
    or 
    ('abcd') in (EMERGENCYAPPROVERUSER,SPECIALAPPROVERUSER)
    or 
    approver_role IN    (SELECT * FROM some_query)
    or 
    CERTIFIER_ROLE IN   (SELECT * FROM some_query)
    or 
    FULFILLMENT_ROLE IN (SELECT * FROM some_query)
)
and entity_type='Entitlement';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM