Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Group sum with condition Post Reply Post New Topic
Author Message
elie234
Groupie
Groupie


Joined: 21 Aug 2009
Online Status: Offline
Posts: 68
Quote elie234 Replybullet Topic: Group sum with condition
     Posted: 26 Aug 2009 at 7:32am
I would like to get a sum for a field by group with a condition for filtering the field (when the field = x). How does one create a formula for a sum with a condition?
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 26 Aug 2009 at 9:09am

If you want to use a Running Total you can use a formula there.

Name it whatever.
Field to summarize: your field
Type of summary = SUM
evaluate set to Use a formula:
add your formula here...field="x" ...the formula must return boolean. When it is True it includes the reocrd for the summary.
reset set to NEver for the REport Footer total.
 
If you are using a variable then use an if then statement...
if field="x" then sumfield else 0
 
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 26 Aug 2009 at 9:17am
Manual Running Total.
 
Create an initializing formulato go in the Group Header that is:
 
whileprintingrecords;
global numbervar nTotal:= 0;
 
Then create a calculation formula to go in the detail section (or whatever section the field to evaluate is in) that is:
 
whileprintingrecords;
global numbervar nTotal:= nTotal + (if {field}='X' then {field} else 0);
nTotal;
 
In the group footer, put the same calculation formula. Since there is no field to evaluate in the footer, it will give you the last amount evaluated. I've also gotten into the routine of creating a third formula, though, to just display the amount in case something funky is going on. The display formula would be:
 
whileprintingrecords;
global numbervar nTotal;
nTotal;
 
I know this seems like an around the universe way of doing it, but sometimes Running Totals don't work properly depending on the fields you use and how you want the information evaluated.
 
 


Edited by FrnhtGLI - 26 Aug 2009 at 9:23am
IP IP Logged
shiloh
Newbie
Newbie


Joined: 13 Oct 2011
Online Status: Offline
Posts: 26
Quote shiloh Replybullet Posted: 29 Apr 2012 at 9:30pm
Originally posted by FrnhtGLI

Manual Running Total.
 
Create an initializing formulato go in the Group Header that is:
 
whileprintingrecords;
global numbervar nTotal:= 0;
 
Then create a calculation formula to go in the detail section (or whatever section the field to evaluate is in) that is:
 
whileprintingrecords;
global numbervar nTotal:= nTotal + (if {field}='X' then {field} else 0);
nTotal;
 
In the group footer, put the same calculation formula. Since there is no field to evaluate in the footer, it will give you the last amount evaluated. I've also gotten into the routine of creating a third formula, though, to just display the amount in case something funky is going on. The display formula would be:
 
whileprintingrecords;
global numbervar nTotal;
nTotal;
 
I know this seems like an around the universe way of doing it, but sometimes Running Totals don't work properly depending on the fields you use and how you want the information evaluated.
 
 
 
Hi
 
I found this formula is very useful for me. But how do I reset the value? I have few category in my report. I need to reset the total when change to next category.
 
Thanks and regards
shiloh
IP IP Logged
yggdrasil
Senior Member
Senior Member
Avatar

Joined: 19 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 150
Quote yggdrasil Replybullet Posted: 30 Apr 2012 at 3:16am
The initializing formula
 
whileprintingrecords;
global numbervar nTotal:= 0;
 
should be in the Header of the Category group, that is where it resets the count.
IP IP Logged
shiloh
Newbie
Newbie


Joined: 13 Oct 2011
Online Status: Offline
Posts: 26
Quote shiloh Replybullet Posted: 01 May 2012 at 7:30pm
My report is not put in detail, is place at one of the group footer. I can get my result out for each product. But how I sum the each category total, the sum figure always out.

This is what I need,
Product Code




UOM


Qty

UnitCost  Total Cost Culmulative Cost























AG
MT
3,036.10
0.04

121,293.9630 121,293.9630
FLY
KG
805.00
0.42

336.1580 121,630.1210

Category A
Grand Total: 121,630.1210























BULK
MT
492,909.00
0.33

161,312.2827 161,312.2827
 PGE
LTR
3,497.03
0.01

22,730.6950 184,042.9777
Category B
Grand Total: 184,042.9777























RP
LTR
4,096.93
0.00

5,735.7020 5,735.7020
65B 
ROLL
11.00
288.16

3,169.8080 8,905.5100

Category C
Grand Total: 8,905.5100























RS
CUYD
4,577.88
0.03

172,033.5982 172,033.5982
SIK
LTR
2,698.44
0.00

12,400.2210 184,433.8192
 SP1
LTR
4,707.84
0.00

10,828.0320 195,261.8512

Category D
Grand Total: 195,261.8512

IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum