Subject Re: Help with sored proc to retrieve lastest records
Author Svein Erling Tysvær
Sorry Gary, ain't got time to help you solve your problem today, but
one thing stared me in the face:

For each branchcode (orderselect) you run the same query on
stats_reports, that means that if you have, say 50 branchcodes, then
you are executing the exact same select 50 times. Since this select is
also using NATURAL, it can be pretty time consuming. I'd rather go for
a select with two joins than doing what you are doing (I just looked
superficially at your code, there may be things that make this
impossible).

HTH,
Set

> begin
> for select orderappend from branches
> where gprsenabled = 'Y'
> order by orderappend
> into :branchcode do
> begin
> for select link, grouplink, description from stats_reports
> where deleted = 'N'
> order by grouplink, link
> into :reportlink, :grouplink, :description do
> begin
> result = 0;
> recdate = NULL;
> rectime = NULL;
> select first(1) result, recdate, rectime
> from stats_results
> where recdate = :recdatein and
> (rectime >= addminute( :rectimein,-60) and rectime <
> addminute( :rectimein,10)) and
> branchcode = :branchcode and
> reportlink = :reportlink
> order by link desc
> into :result, :recdate, :rectime;
> suspend;
> end
> end
> end
>
> PLAN (BRANCHES ORDER I_BRANCHES_ORDERAPPEND)
> PLAN SORT ((STATS_REPORTS NATURAL))
> PLAN (STATS_RESULTS ORDER I_STATS_RESULTS_LINK_SORT)