在Oracle SQL中对分区进行排名

时间:2017-02-10 07:23:17

标签: sql oracle window-functions rank

以某种方式我获取之前的数据(BLAB)...但没有数据(ALAB).... 因此,患者通常在手术前和手术后进入实验室 我的代码能够捕获之前但不是之后....

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 个答案:

答案 0 :(得分:-1)

如果您指的是行的显示顺序,那么您需要一个ORDER BY子句