简体   繁体   中英

Distinct keyword not fetching results in Oracle

I have the following query where I unique records for patient_id, meaning patient_id should not be duplicate. Each time I try executing the query, seems like the DB hangs or it takes hours to execute, I'm not sure. I need my records to load quickly. Any quick resolution will be highly appreciated.

SELECT DISTINCT a.patient_id, 
      a.study_id, 
      a.procstep_id, 
      a.formdata_seq, 
      0, 
      (SELECT MAX(audit_id) 
      FROM audit_info 
      WHERE patient_id =a.patient_id 
      AND study_id     = a.study_id 
      AND procstep_id  = a.procstep_id 
      AND formdata_seq = a.formdata_seq 
      ) AS data_session_id 
      FROM frm_rg_ps_rg a, 
      PATIENT_STUDY_STEP pss 
      WHERE ((SELECT COUNT(*) 
      FROM frm_rg_ps_rg b 
      WHERE a.patient_id = b.patient_id 
      AND a.formdata_seq = b.formdata_seq 
      AND a.psdate      IS NOT NULL 
      AND b.psdate      IS NOT NULL 
      AND a.psresult    IS NOT NULL 
      AND b.psresult    IS NOT NULL) = 1) 
      OR NOT EXISTS 
      (SELECT * 
      FROM frm_rg_ps_rg c 
      WHERE a.psdate                               IS NOT NULL 
      AND c.psdate                                 IS NOT NULL 
      AND a.psresult                               IS NOT NULL 
      AND c.psresult                               IS NOT NULL 
      AND a.patient_id                              = c.patient_id 
      AND a.formdata_seq                            = c.formdata_seq 
      AND a.elemdata_seq!                           =c.elemdata_seq  
      AND a.psresult                               != c.psresult 
      AND ((SELECT (a.psdate - c.psdate) FROM dual)>=7 
      OR (SELECT (a.psdate - c.psdate) FROM dual)  <=-7) 
      ) 
      AND a.psresult IS NOT NULL 
      AND a.psdate   IS NOT NULL;

For start, you have a cartesian product with PATIENT_STUDY_STEP (pss).
It is not connected to anything.


select  *

from   (select  t.*
               ,count (*) over (partition by patient_id) as cnt

        from    frm_rg_ps_rg t 
        ) t

where   cnt = 1
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM