简体   繁体   中英

Oracle selecting object not in as group/collection

There is a table containing credentials, and an audit table containing all the actions involving each credential. I am trying to select all credentials that have not had a particular activity. Say activity 8 is the activity in question.

select t1.credential
from t1 join t2 on t1.credential=t2.credential
where t2.activity <> 8;

just selects all the individual credentials that are NOT 8. Seems I need to do a collection first to determine which credential has no activity of 8, to group all credentials to see which of them have no activity of 8?

Any help or suggestions greatly appreciated.

This should work:

SELECT *
FROM t1
WHERE NOT EXISTS(SELECT 1 FROM t2
                 WHERE credential = t1.credential
                 AND activity <> 8)

Here is an approach using aggregation:

select t1.credential
from t1 join
     t2
     on t1.credential = t2.credential
group by t1.credential
having sum(case when t2.activity = 8 then 1 else 0 end) = 0;

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