Im currently working on troubleshooting an old job which is taking long in running the query. The old job uses the first query but I have been testing using the second query.
Differences between:
Select Max(Cl1) as Tab,
Max(Cl2) as Tb,
Customer
From TableA
group by Customer
vs
Select Customer,
Tab,
tb
From
(Select Customer,
Tab,
tb,
Rank() over (partition by Customer order by Cl1 desc) rk1,
Rank() over (partition by Customer order by Cl2 desc) rk2
From TableA) X
Where X.rk1 = 1 and X.rk2 = 1
Tab Tb Customer
A45845 100052 Shin
A45845 100053 Shin
A45845 100054 Reek
The table will always have value (no nulls or blank value) for both Tab and Tb columns. Tab is not unique to a particular customer. Tb is a sequential and continuously increasing integer with no duplicates possible (unique). The latest Tab value for a customer will also have the most recent Tb as well.
Though the results are the same, is there something I may not be considering when changing the query in this case?
Edit: Fixed errors on second query when building example and not using real column or table names. Also explanded on scenario. My apologies about the updated info and fix in original post, was called before I even had a chance to double check it.
This would be the equiv query, but I doubt it would be more efficient. @Damien_The_Unbeliever please let me know if I’m wrong again. (Distinct added for the scenario where there are multiple Cl1 and Cl2 rows with the same value. This can be removed if the primary key is across customer, Cl1 and Cl2.)