NOT IN query not working, SQL Server 2008

前端 未结 5 1193
伪装坚强ぢ
伪装坚强ぢ 2021-01-27 09:15

\"enter

The first part of the query before not in runs and gives me a list of

相关标签:
5条回答
  • 2021-01-27 09:50

    You need to specify what field is not in the second query

    and Patient.patientid not in
    
    0 讨论(0)
  • 2021-01-27 09:53

    Purely stylistic: you can "squeeze out" the patientICD*tblICD product, and put it into a CTE, and reference that twice, like: (untested)

    WITH zzz AS (
            SELECT pic.patientid , pic.admissiondate , pic.dischargedate
            , tab.ICD_ID , tab.descrip
            FROM patientICD pic
            JOIN tblICD tab ON pic.primarycode = tab.ICD_ID
            )
    SELECT DISTINCT p.patientid
    FROM  Patient p
    JOIN zzz one ON one.patientid = p.patientid
                 AND one.admissiondate = p.admissiondate 
                 AND one.dischargedate = p.dischargedate 
    WHERE one.descrip LIKE N'%diabetes%'
    AND p.patientid NOT IN (
            SELECT two.patientid 
            FROM zzz two
            WHERE two.admissiondate = p.admissiondate
            AND two.dischargedate = p.dischargedate
            AND two.icd_id LIKE N'25000'
            );
    

    NOTE: I don't like the LIKE N'25000'. I think an exact match would be fine. And the icd_id-field should be numeric, probably. And the {admissiondate,dischargedate} pair should be modelled out, too; possibly by using a diagnosis_id or incident_id.

    0 讨论(0)
  • 2021-01-27 09:57

    Did you mean to write this?

    WHERE     (tblICD.descrip LIKE N'%diabetes%') and Patient.patientid not in
    

    UPDATE

    Would it be possible to rewrite the entire thing as this?

    SELECT distinct  Patient.patientid
    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'%diabetes%' AND tblICD.icd_id NOT LIKE N'25000'
    
    0 讨论(0)
  • 2021-01-27 10:02

    I believe you need to specify the column that the not in is looking at. So according to your script I think you would want and Patient.patientid not in

    0 讨论(0)
  • 2021-01-27 10:10

    You forgot a field before NOT.

    0 讨论(0)
提交回复
热议问题