Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

How to calculate sums inside groups

edited August 2003 in General
Hi,

i have a question about how to correctly (and efficiently) calculate
subtotals inside a group on a report. Inside a group i print some records.
Let's say that i have some records which have in their ID field the value 1,
some have the value 2 and some the value 3. I'd like to calculate three
sums - the sums for records of the same type (the total of a field for
records of type 1, type2 and type 3).

Example:

Group #1:
--------------
ID Value
1 1
2 4
1 1
3 1
2 1
--------------
Sum for "1": 2
Sum for "2": 5
Sum for "3": 1


Group #2:
--------------
ID Value
1 2
2 1
1 1
3 1
2 1
--------------
Sum for "1": 3
Sum for "2": 2
Sum for "3": 1

.
.
.


I don't know how to do that correctly. I already tried in many ways. At
first i set the report type to TwoPass and in the first pass i calculated
the sums (every time a record was printed in the detail, i summed it in the
appropriate local variable) - here i also encountered the problem of how to
reset the varibles in delphi when a new group was to be print. In the group
footer i assigned those values to three labels, but the sum wasn't correct,
since the sum of the last group was present on all other groups. Is it
possible at all to assign a value to a label in the first pass and then
leave it untouched?

I thought of something like:

if report1.FirstPass = true then
label1.caption:= floattostr(sum1);

..but it doesn't work :-(


Next i tried to solve the problem using three TppVarables in the group
footer. They had the Timing for the calculation set to Group end. In the
OnCalc event i assigned the values of the local varables (which contain the
sums) to them and reset the local varables, so i could calculate sums for
the next group. This also doesn't work. I also noticed that when i assign
the value to the TppVarables, the value is displayed in the next group, not
in the current. Why?? (The sums are shifted by one group ahead).



After all that, i don't know what else to try - i spent a lot of time trying
to make the report work correctly, but unsuccessfully. The solution with the
TppVariables seems the most appropriate...if it worked.


* Does anyone have any suggestions on how to calculate such sums (general
ideas are also appreciated)? How would you solve this problem (approach)?
* Is it possible to fix the solution with the TppVarables (values shifted by
one group ahead)?
* Why does a label have the same value across the whole report, although i
set its caption for every group in the report's first pass (it seems that
TppVariables don't have such problems) -> i'll ask differently: is it
possible to set a label's caption to a value (for each group or for each
page) during the report's first pass and then to have this value permanently
written? e.g. if i have a report of 10 pages and during the first pass i set
the value of a label to the current page number, is it possible that on the
first page the label would have the caption "1", on the second page it would
be "2".... (and not the caption "10" on all 10 pages) ? (Of course, for page
numbering i use a SystemVariable - this was only an example). If i get an
aswer to this, maybe i can solve the problem.


Any suggestions would be very very appreciated

Thanx in advance

Comments

  • edited August 2003
    There?s a property of the Variable called ResetType to set it to zero again.
    You don?t need to set the report to TwoPass, with three ppVariables you set
    to CalcType = veTraversal and the proper ResetType (GroupEnd i guess) all
    should work fine.

    --
    Guillermo Casta?o A.
    www.GrupoMillennium.com
  • edited August 2003

  • edited August 2003
    Hello,

    1. Check out the example below on the best way to calculate totals in
    subreports and grand totals based on subreports. You will need to use a
    number of TppVariable components and their OnCalc events to produce the
    results you are after.

    http://www.digital-metaphors.com/tips/TotalsFromSubReport.zip

    2. If you are trying to number each page in your report, try using a
    TppSystemVariable component. This can be set up to number the pages for you
    automatically. If I'm completely on the wrong track, please let me know
    exactly what you are trying to accomplish and I'll find a solution for you.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited August 2003
    Hi,

    i already solved the problem using a DBCalc variable in the subreport and
    having some code which controls it. Although, I'll look at the example you
    suggested. Thanx anyway.
This discussion has been closed.