I have the following data table:
patient_id disease
hashA HIV-2
hashA COPD-1
hashA diabetes
hashB diabetes
hashB FSGS
.
.
.
I would like to extract all the rows for patients who have diabetes, but not COPD or HIV.
So far, I have:
select t.*
from patient_table t
where exists (select 1
from patient_table t2
where t2.disease like '%diabetes%' and
t2.disease not like '%HIV%' and
t2.disease not like '%COPD%' and
t.patient_id = t2.patient_id
)
This however does not exclude all the rows for patients with COPD and HIV. Is there an alternative?