不同的关键字无法在Oracle中获取结果

时间:2016-12-15 15:59:16

标签: sql oracle

我有以下查询,其中我是patient_id的唯一记录,这意味着patient_id不应该重复。每次我尝试执行查询时,似乎数据库挂起或执行需要数小时,我不确定。我需要我的记录快速加载。任何快速解决方案都将受到高度赞赏。

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;

1 个答案:

答案 0 :(得分:0)

首先,您的笛卡尔积为PATIENT_STUDY_STEP(pss) 它与任何东西都没有联系。

select  *

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

        from    frm_rg_ps_rg t 
        ) t

where   cnt = 1
;