SUM of a specific field

Viewed 21

I wonder if you can help me in this situation. What i want is: If a specific field has a specific value (example, if on the column "item" one of the fields is equal to "item1") I want the sum of the cost of that "item1" (in this case, the cost is a fixed value, so I want the number of times that specific item appears x that value) otherwise I want it to return 0

can you please help me? thanks

I've tried something like this but no good:

if ({WGCDocLinhas.artigo}='TAXA') >= 1 then select SUM {WGCDocLinhas.precounit} from WGCDocLinhas where WGCDocLinhas.artigo}='TAXA'

1 Answers

There are a few ways you can do this in Crystal Reports. My preference for what you describe would be to first group the report on the Item column. This will reorder your dataset so that you can deal with each value of Item as a group and will also create a section in your report for Group Header and Group Footer.

Place the Item field in the Group Header, then place the field for Cost in the details section. Now right click on the Cost field and select Insert Summary to create a Summary Field based on the Cost values. You are going to need 2 summary fields, one for the COUNT of the number of time the Item appears in the details section for each value of Item, and another for the SUM of the Cost values.

If there are no detail records to count for a value of Item, the summary field will either show a value of zero, or they may just be NULL value and not show anything at all. This will depend upon how you have setup your Crystal Reports environment.

Related