2012-02-23 34 views
1

我有下面的查詢。 SOFTWARE_DEVELOPMENT_CYCLE有多行,但我對最新版本感興趣。Oracle - 用max()重構子查詢

我想重寫查詢,以便我不使用子查詢。我曾嘗試過使用DENSE_RANK LAST ORDERY BY,但無濟於事。

有人建議嗎?謝謝。

SELECT SOF.VENDOR, 
     SOF.NAME, 
     LAN.LANGUAGE, 
     SOF.VERSION, 
     SDC.STATUS, 
     SDC.SOF_DC_ID 
    FROM SOFTWARE SOF 
    JOIN SOFTWARE_LANGUAGES SL 
    ON (SL.SOF_SOF_ID = SOF.SOF_ID) 
    JOIN LANGUAGES LAN 
    ON (SL.LAN_LAN_ID = LAN.LAN_ID) 
    JOIN SOFTWARE_DEVELOPMENT_CYCLE SDC 
    ON (SDC.SOF_LAN_SOF_LAN_ID = SL.SOF_LAN_ID) 
WHERE SDC.SOF_DC_ID IN (SELECT MAX(SDC2.SOF_DC_ID) 
          FROM SOFTWARE_DEVELOPMENT_CYCLE SDC2 
          WHERE SDC2.SOF_LAN_SOF_LAN_ID = SL.SOF_LAN_ID) 
ORDER BY SOF.VENDOR, 
      SOF.NAME, 
      LAN.LANGUAGE, 
      SOF.VERSION; 

回答

2

你可以做這樣的事情,以避免打SOFTWARE_DEVELOPMENT_CYCLE表中的第二次

SELECT vendor, 
     name, 
     language, 
     version, 
     status, 
     sof_dc_id 
    FROM (SELECT SOF.VENDOR, 
       SOF.NAME, 
       LAN.LANGUAGE, 
       SOF.VERSION, 
       SDC.STATUS, 
       SDC.SOF_DC_ID, 
       RANK() OVER (PARTITION BY sl.sdf_lan_id 
           ORDER BY sdc.sdf_dc_id DESC) rnk 
      FROM SOFTWARE SOF 
      JOIN SOFTWARE_LANGUAGES SL 
      ON (SL.SOF_SOF_ID = SOF.SOF_ID) 
      JOIN LANGUAGES LAN 
      ON (SL.LAN_LAN_ID = LAN.LAN_ID) 
      JOIN SOFTWARE_DEVELOPMENT_CYCLE SDC 
      ON (SDC.SOF_LAN_SOF_LAN_ID = SL.SOF_LAN_ID)) 
WHERE rnk = 1 
ORDER BY VENDOR, 
      NAME, 
      LANGUAGE, 
      VERSION; 

RANK解析功能劃分的結果通過sl.sdf_lan_id設置。然後,對於每個不同的sl.sdf_lan_id,它將根據sdc.sdf_dc_id的降序爲該行分配一個數字等級。這意味着對於特定的sl.sdf_lan_id具有最大sdc.sdf_dc_id的行將具有爲RANK的1。外部WHERE rnk=1謂詞然後僅選擇具有該最大值的行。這應該完成與MAX子查詢正在完成的相同的事情。

+0

謝謝。你能否解釋你的過程,以決定如何做到這一點? – retrodev 2012-02-23 16:48:22