select distinct patientID
from [dbo]..HPrecords
where ptprocess = 'refill'
and pTDescription <> 'Success'
and patientID is not null
and patiententrytime > '2021-04-06'
and patientID not in (
select distinct patientID
from [dbo]..HPrecords
where ptprocess = 'embossing'
and ptDescription = 'Success'
and patientID is not null
and patiententrytime > '2021-04-06'
)
So I want to use a NOT IN
feature in SQL to filter out the patients that haven't received their refill medication yet. A patient can be refilled multiple times, the first time can fail, but the second or third time it can be successful. So there can be multiple rows.
So I just want to write a query that will filter out and get me the patientID that DID NOT SUCCEED in getting refill at all no matter how many times.
Is this the best way to write it, my current query is still running, I think the logic is wrong?
I want to try to write this query without CTE or temp table just as an exercise.
Sample output:
PatientID
151761
151759
151757
151764