Consultor Eletrônico



Kbase P61302: FOR EACH and FOR LAST return different results
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   2/8/2011
Status: Verified

SYMPTOM(s):

FOR EACH and FOR LAST return different results

The query:

FOR LAST men NO-LOCK
WHERE men.idt_men_itm_par = 30
BY men.num_vlg :
DISPLAY men.num_vlg idt_men_itm.
END.

displays the record with the highest primary key value, while the query:

FOR EACH men NO-LOCK
WHERE men.idt_men_itm_par = 30
BY men.num_vlg :
DISPLAY men.num_vlg idt_men_itm.
END.

displays the record with the highest value of the expression after the BY clause.

FACT(s) (Environment):

All Supported Operating Systems
Products / Versions

CAUSE:

This is expected and documented behavior as seen in the "Progress Language Reference":

LAST

Uses the criteria in the record-phrase to find the last record in the table that meets that criteria. Progress finds the last record before sorting.

FOR LAST customer BY credit-limit:
DISPLAY customer.
END.

The procedure above displays the customer with the highest customer number (cust-num is the primary index of the customer table), not the customer with the highest credit-limit.

A procedure that displays the customer with the highest credit-limit looks like the following.

FOR EACH customer BY credit-limit DESCENDING:
DISPLAY customer.
LEAVE.
END.

See the NOTES section for more information on using this option.

FIX:

Use either FOR EACH (if the record with the highest value of the expression after the BY clause is needed) or FOR LAST (if the record with the highest primary key value is needed) depending on your requirements.