我有下面的查询。 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;
答案 0 :(得分: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
的降序为行指定数字排名。这意味着特定sdc.sdf_dc_id
具有最大sl.sdf_lan_id
的行的RANK
为1.外WHERE rnk=1
谓词然后仅选择具有该最大值的行。这应该完成与MAX
子查询完成相同的事情。