I need to display Y or N when displaying rows in a report based on the max value for them on their version. If they have multiple versions all of them will display 'N' but the max (last version) will display 'Y'.
I have tried with case when and max but always I'm getting 'Y'
case when version = (select max(version) from t_proj_f where version = t.version) then 'Y' else 'N' end Is_last_version
Here is fiddle in case anybody can take a look
http://sqlfiddle.com/#!4/813501/4
I'm new on oracle sql any help is appreciated.
Best Answer
Analysis
Your inner SQL statement is always TRUE (for non-null values). You are probably wanting the
max(version)
for eachproject_id
, not eachversion
.Additionally, these scalar sub-queries are known to be slow at times.
Solution
Use an Analytical function.
PLAN vs
Yours, right side, is doing two Full Table Scans vs my one. This becomes worse with larger tables.
Code
code review note: always use the appropriate table alias on ALL columns, especially if the column could be misinterpreted.