简体   繁体   中英

How can I find records that are exclusive to a particular condition, but not others?

I have been tasked to find all drugs (NDC) within a pharmacy database that are used exclusively by an array of customer IDs but not others. Basically find drugs that have no overlap between customers (array) so that we can trim unneeded inventory used by previous customers. Columns needed are NDC (drug code), DrugName.

Thanks for the assistance. Through some research and using another method, I was able to find the answer from another post . I ended up selecting from one subquery and left joining another to find the records needed.

Hmmm. . .

select NDC, DrugName
from t
group by NDC, DrugName
having sum(case when customerid in ( . . . ) 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