Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculating summary totals before detail lines

Status
Not open for further replies.

Ju5

Programmer
May 25, 2007
85
PH
I have a order report that prints the summary lines first before it prints the detail lines. The report works fine so long as there is only one line per order.

The problem comes up when there are multiple lines for one order. Since the summary is written first before the detail only the first line in the order is included in the summary calculation. The report looks like this:

--------------------------------------------------
Order 1 total: n
--------------------------------------------------
Order 1 line 1 details: amount n

--------------------------------------------------
Order 2 total: x(this should be the sum of x+y+z)
--------------------------------------------------
Order 2 line 1 details: amount x
Order 2 line 2 details: amount y
Order 2 line 3 details: amount z

The logic of the program is such that after the initial CHAIN to find the order the summary line print is immediately called before the detail lines have a chance to get processed.

Any help would be appreciated. Thanks in advance.
 
Well,, at this point,, you have some choices, none of them pretty. After the chain to get the order number,, you have to process all the detail, to get the summary total. Or you could pre-process the file, and build summary by order totals, put them in a temp. file,, and chain to that file,, for your totals. You could do somthing more traditional, and move the totals to "after", the detail. Are you using the rpg cycle, or controlling everything?
 
This is backward from the way it's normally done. You could pass through the data twice. Once for order total and once for the detail.
 
Well,, if you have seen crystal reports, as a reporting tool.. you can drag and drop the "group", totals, at the group header line. Not a traditional way of repsenting the natual flow of the data, but each to their own, so this could be why the question.
 
The easiest way is to create a subroutine every time there is a change of order# or for the first order#. This subroutine is in fact a set of SQL statement to summarize the amount by order. Other suggestion above are valid too.
 
i FINALLY GOT IT!

Below is the pseudo code for the program I'm working on.


FILE12 - a LF containing Orders
SUMFILE - the summary file contains the Gross weight of the orders

Below is the structure of the orders as soted in FILE12

Order1 Line 1 Inc#1 quantity

Order1 Line 2 Inc#1 quantity
Order1 Line 2 Inc#2 quantity

Key KLIST
KFLD Order
KFLD Inc#

read FILE12
Execute $Init(this subroutine initializes variables, moves Inc# to TmpInc# and executes $Comput)
DOU EOF
Inc# casne TmpInc# $Comput
Print detail line
Read File12
Enddo


$Comput
Chain to SUMFILE using Key to get the Gross Weight
Use Gross Weight to compute the other fields used for the Summary
Print Summary
z-add 0 to total
ENDSR

What I did:
Added FILE13 adding Prefix SV to differentite from FILE12

in $Comput after the Chain to Sumfile
CHAIN to FILE13 using Key indicator 23
If *IN23 is '0'
Dow *in23 = '0'
if SV_Inc# = Inc#
add sv_qty to total
endif
Reade FILE13 *IN23 if eof
Enddo

The order line with multiple Inc# gets accumulated and all the lines with single orders also have the correct computation.

Couldn't have done it without the inspiration your suggestions. Thanks to everyone!
 
Just a suggestion, Ju5. You don't need to execute $Init. Set up your $Init in *INZSR. It executes automically when the program starts; no need to call it. It's minor but just a thought.
 
An easier way is summarize by detail line
reset after group.
then drag summary group total and place it in the header,
it works like a charm.
d
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top