2017-06-19 53 views
2

我有一個使用WITH子句的查詢,能夠計算SQL結果中的某些標誌(IS_EMPLOYE,IS_AFFILIE)。但是我需要在不使用WITH子句的情況下找到與此查詢等效的內容,因爲執行SQL的軟件不允許使用WITH子句。 (我已經考慮過使用視圖來代替WITH子句,但我沒有權限創建或更改數據庫中的任何內容)。Oracle SQL - 不使用WITH子句獲得相同結果

這是查詢:

WITH 
    pty_id AS (
    SELECT pty.PARTY_ID 
    FROM PTY_ROL_REL prr 
    JOIN PARTY pty ON prr.PRREL_PTY_ID = PTY.PARTY_ID 
    WHERE prr.PTY_ROL_REL_ID = <an ID provided by a variable> 
    ), 
    affi AS (
    SELECT prr.PRREL_PTY_ID, 
    Count(*) ISAFFILIE 
    FROM PTY_ROL_REL prr 
    JOIN PTY_ROL_STA_REL prsr 
    ON prr.PTY_ROL_REL_ID = prsr.PRSRE_PTY_ROL_REL_ID AND prsr.PRSRE_PTY_STA_CD = '01' 
    WHERE prr.PRREL_PTY_ROL_CD IN ('001','002') 
    GROUP BY prr.PRREL_PTY_ID 
    ), 
    empl AS (
    SELECT PRREL_PTY_ID, 
    Count(*) ISEMPLOYE 
    FROM PTY_ROL_REL 
    GROUP BY PRREL_PTY_ID 
    ), 
SELECT 
PARTY.PARTY_ID PKEY_SRC_OBJECT, 
ROWIDTOCHAR(PARTY.ROWID) SRC_ROWID, 
PARTY.PARTY_ODS_MODF_DAT LAST_UPDATE_DATE, 
decode(nvl(affi.ISAFFILIE, 0), 0, 0, 1) AS IS_AFFILIE, 
decode(nvl(empl.ISEMPLOYE, 0), 0, 0, 1) AS IS_EMPLOYE 
FROM PARTY 
JOIN pty_id ON PARTY.PARTY_ID = pty_id.PARTY_ID 
JOIN affi ON pty_id.PARTY_ID = affi.PRREL_PTY_ID 
JOIN empl ON pty_id.PARTY_ID = empl.PRREL_PTY_ID; 

回答

5

正如你所引用的每個CTE只有一次,你可以重寫它們作爲派生表:

SELECT party.party_id pkey_src_object, 
     rowidtochar(party.rowid) src_rowid, 
     party.party_ods_modf_dat last_update_date, 
     decode(nvl(affi.isaffilie, 0), 0, 0, 1) as is_affilie, 
     decode(nvl(empl.isemploye, 0), 0, 0, 1) as is_employe 
FROM PARTY 
    JOIN (
     SELECT pty.PARTY_ID 
     FROM pty_rol_rel prr 
     JOIN party pty ON prr.PRREL_PTY_ID = PTY.PARTY_ID 
     WHERE prr.PTY_ROL_REL_ID = <an ID provided by a variable> 
) pty_id ON party.party_id = pty_id.party_id 
    JOIN (
     SELECT prr.PRREL_PTY_ID, 
      Count(*) ISAFFILIE 
     FROM pty_rol_rel prr 
     JOIN pty_rol_sta_rel prsr ON prr.PTY_ROL_REL_ID = prsr.PRSRE_PTY_ROL_REL_ID 
            AND prsr.PRSRE_PTY_STA_CD = '01' 
     WHERE prr.PRREL_PTY_ROL_CD IN ('001','002') 
     GROUP BY prr.PRREL_PTY_ID 

) affi ON pty_id.PARTY_ID = affi.PRREL_PTY_ID 
    JOIN (
     SELECT PRREL_PTY_ID, 
      Count(*) ISEMPLOYE 
     FROM pty_rol_rel 
     GROUP BY PRREL_PTY_ID 
) empl ON pty_id.PARTY_ID = empl.PRREL_PTY_ID; 
相關問題