2017-02-10 77 views
0

某種程度上,我正在獲取之前(BLAB)的數據...但是沒有數據(ALAB)....
因此,病人通常在手術前和手術後獲得實驗室 我的代碼能夠捕捉德前而不是後....Oracle SQL中的分區排序

WITH BLAB AS 
(
    SELECT /*+PARALLEL*/ 
     PAT.PATID_CD, 
     PAT.NOTED_DT, 
     LAB.COLLECTION_DT, 
     LAB.RESULT_DE, 
     LAB.UNI_LAB_CD, 
     LAB.PROCEDURE_DE, 
     RANK() OVER (PARTITION BY LAB.PATID_CD, PAT.NOTED_DT ORDER BY LAB.COLLECTION_DT DESC) AS RNK 
    FROM KPHC_PROBLEM_LIST_CN PAT 
    LEFT OUTER JOIN LAB 
     ON 
     PAT.PATID_CD = LAB.PATID_CD 
     AND LAB.REGION_CD = PAT.REGION_CD 
     AND LAB.REGION_CD ='CN' 
     AND LAB.COLLECTION_DT BETWEEN TO_DATE ('01-JAN-12') AND TO_DATE ('01-JAN-16') 
     AND (PROCEDURE_DE LIKE '%EGFR%' 
      OR PROCEDURE_DE LIKE '%INR%' 
      OR PROCEDURE_DE LIKE '%HEMOG%' 
      OR PROCEDURE_DE LIKE '%HEMATR%' 
      OR PROCEDURE_DE LIKE '%PLATE%' 
      OR PROCEDURE_DE LIKE '%CRCL%' 
      OR PROCEDURE_DE LIKE '%CREATI%' 
      OR PROCEDURE_DE LIKE '%PTT%' 
      OR PROCEDURE_DE LIKE '%ANTI-XA%') 
     AND LAB.COLLECTION_DT < PAT.NOTED_DT 
) 
, ALAB AS 
(
    SELECT /*+PARALLEL*/ 
     PAT.PATID_CD, 
     PAT.NOTED_DT, 
     LAB.COLLECTION_DT, 
     LAB.RESULT_DE, 
     LAB.UNI_LAB_CD, 
     LAB.PROCEDURE_DE, 
     RANK() OVER (PARTITION BY LAB.PATID_CD, PAT.NOTED_DT ORDER BY LAB.COLLECTION_DT ASC) AS RNK 
    FROM KPHC_PROBLEM_LIST_CN PAT 
    LEFT OUTER JOIN LAB 
     ON 
     PAT.PATID_CD = LAB.PATID_CD 
     AND LAB.REGION_CD = PAT.REGION_CD 
     AND LAB.REGION_CD ='CN' 
     AND LAB.COLLECTION_DT BETWEEN TO_DATE ('01-JAN-12') AND TO_DATE ('01-JAN-16') 
     AND LAB.UNI_LAB_CD = 'HGB_' 
     AND (PROCEDURE_DE LIKE '%EGFR%' 
      OR PROCEDURE_DE LIKE '%INR%' 
      OR PROCEDURE_DE LIKE '%HEMOG%' 
      OR PROCEDURE_DE LIKE '%HEMATR%' 
      OR PROCEDURE_DE LIKE '%PLATE%' 
      OR PROCEDURE_DE LIKE '%CRCL%' 
      OR PROCEDURE_DE LIKE '%CREATI%' 
      OR PROCEDURE_DE LIKE '%PTT%' 
      OR PROCEDURE_DE LIKE '%ANTI-XA%') 
     AND LAB.COLLECTION_DT > PAT.NOTED_DT 
) 
SELECT /*+PARALLEL*/ 
    BLAB.PATID_CD, 
    BLAB.NOTED_DT  AS PROCEDURE_DATE, 
    BLAB.PROCEDURE_DE AS LAB_TYPE, 
    BLAB.COLLECTION_DT AS DATE_TAKEN_AFTER, 
    BLAB.RESULT_DE  AS LAB_BEFORE, 
    ALAB.COLLECTION_DT AS DATE_TAKE_AFTER, 
    ALAB.RESULT_DE  AS LAB_AFTER 
FROM 
    ALAB, 
    BLAB 
WHERE 
    ALAB.PATID_CD = BLAB.PATID_CD 
    AND ALAB.NOTED_DT = BLAB.NOTED_DT 
    AND ALAB.RNK = 1 
    AND BLAB.RNK = 1; 

enter image description here

+1

提供e xample數據和預期輸出。目前尚不清楚,結果集中缺少哪些數據。 – Serg

+0

嗨Serg ...謝謝發佈了一個結果.... – LEARNDATAsCI

+0

只是ALAB的'LEFT JOIN'在LAB中找不到匹配的數據。儘管如此,'RANK()OVER(通過空NULL分區)'返回1 – Serg

回答

-1

如果你指的是在你行的顯示順序,那麼你需要一個ORDER BY條款