I have the query below. SOFTWARE_DEVELOPMENT_CYCLE has multiple rows, but I’m interested in the latest.
I would like to rewrite the query so that I don’t use a subquery. I have attempted it with DENSE_RANK LAST ORDERY BY, but to no avail.
Could somebody advise? Thank you.
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;
You could do something like this to avoid having to hit the
SOFTWARE_DEVELOPMENT_CYCLEtable a second timeThe
RANKanalytic function is partitioning the result set bysl.sdf_lan_id. Then for each distinctsl.sdf_lan_id, it is assigning a numeric rank to the row based on the descending order ofsdc.sdf_dc_id. That means that the row with the largestsdc.sdf_dc_idfor a particularsl.sdf_lan_idwill have aRANKof 1. The outerWHERE rnk=1predicate then selects only the rows that have that maximum value. That should accomplish the same thing that yourMAXsubquery is accomplishing.