Note that there are some explanatory texts on larger screens.

plurals
  1. POWHERE NOT EXISTS not working SQL Server 2008
    primarykey
    data
    text
    <p><img src="https://i.stack.imgur.com/LBoyv.jpg" alt="enter image description here"></p> <p>This is a related question to <a href="https://stackoverflow.com/questions/11127684/not-in-query-not-working-sql-server-2008">this</a> but with EXISTS instead of IN</p> <pre><code>SELECT distinct Patient.patientid FROM Patient P JOIN patientICD pICD ON P.patientid = pICD.patientid AND P.admissiondate = pICD.admissiondate AND P.dischargedate = pICD.dischargedate JOIN tblICD ON pICD.primarycode = tblICD.ICD_ID WHERE tblICD.descrip LIKE N'%diabetes%' AND patient.patientID not in ( SELECT distinct Patient.patientid FROM Patient P JOIN patientICD pICD ON P.patientid = pICD.patientid AND P.admissiondate = pICD.admissiondate AND P.dischargedate = pICD.dischargedate JOIN tblICD ON pICD.primarycode = tblICD.ICD_ID WHERE tblICD.icd_id =N'25000' ) </code></pre> <p>This query works fine, but when I try to add another field to the select expressions I have to use <code>WHERE EXISTS</code> and the following query returns 0 results, while the above query returns 130. I'm trying to add the tblicd.icd_id field to the result set from the first query and I'm doing something wrong.</p> <pre><code>SELECT DISTINCT Patient.patientid, tblicd.icd_id FROM Patient INNER JOIN patientICD ON Patient.patientid = patientICD.patientid AND Patient.admissiondate = patientICD.admissiondate AND Patient.dischargedate = patientICD.dischargedate INNER JOIN tblICD ON patientICD.primarycode = tblICD.ICD_ID WHERE tblICD.descrip LIKE N'%TextOfNote%' and not exists ( SELECT DISTINCT Patient.patientid, tblicd.icd_id FROM Patient INNER JOIN patientICD ON Patient.patientid = patientICD.patientid AND Patient.admissiondate = patientICD.admissiondate AND Patient.dischargedate = patientICD.dischargedate INNER JOIN tblICD ON patientICD.primarycode = tblICD.ICD_ID WHERE tblICD.icd_id = N'25000' ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload