Hello developer friends! This week I want to talk about the ACCUM function in Progress 4GL.
This function works similarly to the SQL SUM function.
Both functions are used to consolidate or calculate the total value of a field based on specific conditions.
For example, if you want to calculate the total invoiced sales amount by customer, the SQL statement would look like this:
SELECT SUM(ih_invoicetotal), ih_bill FROM ih_hist WHERE ih_domain = 'domain' AND YEAR(ih_inv_date) = 2019 GROUP BY ih_bill
Now, the same example written in Progress 4GL:
FOR EACH ih_hist WHERE ih_domain = 'domain' AND YEAR(ih_inv_date) = 2019 NO-LOCK BREAK BY ih_bill: ACCUMULATE ih_invoicetotal (TOTAL BY ih_bill). IF LAST-OF(ih_hist.ih_bill) THEN DISPLAY ACCUM TOTAL BY ih_bill ih_invoicetotal ih_bill. END.
As shown in the images, both approaches generate the same result.
The Progress functions used in this example were:
- ACCUMULATE: Allows us to continuously sum or accumulate the value of a field from our table.
- TOTAL BY: Indicates which field will be used to group or consolidate the totals. Besides TOTAL, you can also use AVERAGE, COUNT, MAXIMUM, and MINIMUM.
- ACCUM TOTAL BY: Used to display the accumulated total on screen.
- LAST-OF: Tells the query to stop once all records for a specific group have been processed. There is also a FIRST-OF option available.
- BREAK BY: Instructs the query to group and process records based on a specific database field.
As a personal anecdote, when I first started learning Progress, I used to search for ways to translate SQL queries into Progress syntax.
Doing that helped me understand the logic before actually coding the solution.
Today, I think differently: the most important thing is understanding the logic independently of the programming language.
That is how programming should work:
first think about the algorithm, then translate it into code.
Don’t forget to share this article and follow us on
Facebook
.
Comments
Post a Comment