SQL Query--is not excluding a segment of my data

I have the following code;

select distinct pt.person_id, p.last_name, p.first_name, p.address_line_1, p.city, p.state, p.zip from 
patient_ pt
INNER JOIN vital_signs_ vs ON vs.person_id = pt.person_id
INNER JOIN patient_problems pp ON pp.person_id = pt.person_id
INNER JOIN lab_results_obx lr ON lr.person_id = pt.person_id
INNER JOIN person p on p.person_id = pt.person_id
where pt.age >= 18 AND vs.BMI_calc >= 22 
and ((lr.obs_id like '25000000^GLUCOSE^' and CAST(lr.observ_value  AS decimal)>100) OR (lr.obs_id like '%FM HEM A1C^HEMOGLOBIN A1C^I%')  
and NOT EXISTS (select NULL from patient_problems p1 where (  description  like 'Diabetes%')
AND pt.person_id = p1.person_id))
order by p.last_name desc

Open in new window


When I check my data--everythings appears correct, except for this;

and NOT EXISTS (select NULL from patient_problems p1 where (  description  like 'Diabetes%')
AND pt.person_id = p1.person_id))

Open in new window


I have patients showing up that have Diabetes listed in their patient problem table.  The exact name of the patient_problem is 'Diabetes Type II' so the like statement should be catching it.

  I had posted this previously and after checking data, thought it was clean--however after I went 20+ names in diabetic patients started showing up.  Thanks!
CMCITDAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
I believe that's due to NULL comparison which doesn't work like that - please try updated code below:

select distinct pt.person_id, p.last_name, p.first_name, p.address_line_1, p.city, p.state, p.zip
from patient_ pt
INNER JOIN vital_signs_ vs ON vs.person_id = pt.person_id
INNER JOIN patient_problems pp ON pp.person_id = pt.person_id
INNER JOIN lab_results_obx lr ON lr.person_id = pt.person_id
INNER JOIN person p on p.person_id = pt.person_id
where pt.age >= 18 AND vs.BMI_calc >= 22
      and ((lr.obs_id like '25000000^GLUCOSE^' and CAST(lr.observ_value  AS decimal)>100) OR (lr.obs_id like '%FM HEM A1C^HEMOGLOBIN A1C^I%')  
      and NOT EXISTS (select person_id from patient_problems p1 where (  description  like 'Diabetes%')
      AND pt.person_id = p1.person_id))
order by p.last_name desc
0
CMCITDAuthor Commented:
Ok--tried that code---same patients are still appearing
0
lcohanDatabase AnalystCommented:
This may make it slower as full pattern search will invalidate any existing index on description column but can you please try again with code below

select distinct pt.person_id, p.last_name, p.first_name, p.address_line_1, p.city, p.state, p.zip
from patient_ pt
INNER JOIN vital_signs_ vs ON vs.person_id = pt.person_id
INNER JOIN patient_problems pp ON pp.person_id = pt.person_id
INNER JOIN lab_results_obx lr ON lr.person_id = pt.person_id
INNER JOIN person p on p.person_id = pt.person_id
where pt.age >= 18 AND vs.BMI_calc >= 22
      and ((lr.obs_id like '25000000^GLUCOSE^' and CAST(lr.observ_value  AS decimal)>100) OR (lr.obs_id like '%FM HEM A1C^HEMOGLOBIN A1C^I%'))  
      and NOT pt.person_id IN (select person_id from patient_problems p1 where ([description] like 'Diabetes%') AND pt.person_id = p1.person_id)
order by p.last_name desc
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CMCITDAuthor Commented:
Sorry was in a clinic in the last couple of days, thanks so much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.