Consultor Eletrônico



Kbase P24608: How to calculate the total of a field in a query
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   7/30/2010
Status: Verified

GOAL:

How to calculate the total of a field in a query

GOAL:

How to get the total of a column from a query

FACT(s) (Environment):

All Supported Operating Systems
Progress 9.x
OpenEdge 10.x

FIX:

The the following code uses the sports2000 database:
DEFINE VARIABLE hq AS HANDLE NO-UNDO.
DEFINE VARIABLE TotalAmount AS DEC.

CREATE QUERY hq.

hq:SET-BUFFERS(BUFFER invoice:HANDLE).
hq:QUERY-PREPARE("for each invoice FIELDS (invoice.amount) no-lock ").
hq:QUERY-OPEN.

TotalAmount = 0.

hq:GET-FIRST().
DO WHILE NOT hq:QUERY-OFF-END:
TotalAmount = TotalAmount + Invoice.amount.
hq:GET-NEXT .
END.
hq:QUERY-CLOSE.

DISPLAY TotalAmount. Please note that this query uses the FIELDS option. In this way you retrieve just the field needed for the evaluation of the total. This is especially useful in client networking when dealing with a large amount of data.