Wednesday, October 5, 2016

crystal reports group sum and total sum

It is very often that a report displays data grouped by some category, displays a group summary at the end of each group and a total summary at the end of the report.

Following is about how to create the formula fields to accumulate the counts and display them.

If it is a simple account such as the sum or count of a single field, you can insert a summary by 

1. Click the Insert on the top menu and select Summary.
2. In the Insert Summary window, choose the field you would like to use, the type of calculation you would like to perform on this field, and the location you want this summary to be on your report.
3. Click OK to close the window.
4. Find the summary on your report, right click on it and select Format Field.
5. Click each tab on the popup Format Editor window to specify the appearance of it.

However, if you want to create a complicated counter or summary that involve more than one field or complex logic, you may following the guide below to create it.

1. Open the Formula Workshop. From top menu, click Report, then select Formula Workshop
2. Create a reset formula, which will set the count to zero when a new group starts to display. Right click on Formula Fields and select new, enter the formula name, and click OK. Enter the following into the lower-right pane.

      Shared Numbervar recordCount;
      recordCount := 0;

For example you want to count on the records which have been created or updated, you can set it like this.

      Shared Numbervar countInserted;
      Shared Numbervar countUpdated;
            countInserted:=0;
            countUpdated :=0;

     Place the formula in group header.

3. Create the increment formula. 

      In general, it is like this.

      Shared Numbervar recordCount;
      recordCount := recordCount + 1;

     As in the above example, it will look like this.

      Shared Numbervar countInserted;
      Shared Numbervar countUpdated;

      if(not isModified({<createDate>}, {<modifyDate>})) then 
      countInserted:=countInserted+1;
      else 
      countUpdated:=countUpdated+1;

      countInserted;
      countUpdated;

      Add the formula to the detail section.


4. Create the display formula(s). 

      In general
  
      Shared Numbervar recordCount;
      Shared Numbervar totalRecordCount;
      totalRecordCount := totalRecordCount + recordCount;
      recordCount;

      As in the example, you need to created two display formulas, one for the newly created records and one for the updated records.

     Add the formula(s) to the group footer.

5. Create the display total formula(s).

      In general

      Shared Numbervar totalRecordCount;
      totalRecordCount;

      As in the example, create two display total formulas, one for the created and one for the updated records.

      Add the formula(s) to the report footer.
      

Reference:

1. How to count number of grouped rows in the Crystal Report


---------------------------------------------------------------------------------------------------

                        

If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment