I am using below query to fetch the top two records for profile_run_key. I am using three almost similar queries to get this done. This means I am traversing the table thrice for the "where" clause. So I think this will take 3(n) time for execution. Alternatively I can use "Order by" but the it will take nlogn time to execute.
SELECT name, weighted_average
FROM idp_weighted_avg
where (profile_run_key =
(SELECT MAX (profile_run_key)
FROM idp_weighted_avg
WHERE SCORECARD_IDENTIFIER = 'U:D8yIYvW6EeGKyklcM7Co1A')
OR profile_run_key =
(SELECT MAX (profile_run_key)
FROM idp_weighted_avg
WHERE SCORECARD_IDENTIFIER = 'U:D8yIYvW6EeGKyklcM7Co1A'
AND profile_run_key <
(SELECT MAX (profile_run_key)
FROM idp_weighted_avg
WHERE SCORECARD_IDENTIFIER =
'U:D8yIYvW6EeGKyklcM7Co1A')))
I was wondering if I can reuse (I don't want to create a temp table) the result of the below sub query? Any alternatives? Sugestions?
SELECT MAX (profile_run_key)
FROM idp_weighted_avg
WHERE SCORECARD_IDENTIFIER = 'U:D8yIYvW6EeGKyklcM7Co1A'