Oracle-使用max()重构子查询



我有下面的查询。SOFTWARE_DEVELOPMENT_CYCLE有多行,但我对最新的感兴趣。

我想重写查询,这样我就不用子查询了。我曾尝试过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;

您可以这样做,以避免第二次时碰到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;

CCD_ 2分析函数将结果集划分为CCD_ 3。然后,对于每个不同的sl.sdf_lan_id,它将根据sdc.sdf_dc_id的降序为行分配一个数字秩。这意味着对于特定sl.sdf_lan_id具有最大sdc.sdf_dc_id的行将具有1的RANK。外部WHERE rnk=1谓词然后只选择具有该最大值的行。这应该完成与MAX子查询相同的事情。

相关内容

  • 没有找到相关文章

最新更新