Search code examples
sqlcrystal-reportscrystal-reports-2011crystal-reports-formulas

Trying to SUM a formula field based on constraints from another field


A little background on the report: This is a productivity report for our employees working at our business. We determine their productivity based on the duration of the visits with clients. Some of our employees offer group sessions. They charge each client within the group, even though they are only giving, for example, one hour of service, they can bill for 10 hours if there are 10 people in the group. We determine what service they gave by service codes.

So, I have two fields in this formula, a service code field and a duration field.

The duration field is initially a STRING field from the database, even though it only gives number data, so I change it to a numberVar. The service code field is also a string field, and it sometimes does contain characters and numbers.

What I need Crystal Reports to do is take the sum of the duration. However, if the service code is, say, "1000", it must first divide the duration by 3 before summing it. This is where I get caught up.

Here's my code for the duration:

local numbervar num1;
If GroupName ({billing_tx_charge_detail.v_SERVICE_CODE})="1530" then 
    num1 := ToNumber({billing_tx_charge_detail.v_duration})/3
else num1 := ToNumber({billing_tx_charge_detail.v_duration})

Then I do a separate formula for the sum, named sumDuration:

Sum(@duration)

I get the error that this field cannot be summarized. After searching Google for two days I have found that Crystal cannot summarize fields or formulas involving constants. If I simply say:

local numbervar num1;
num1 := ToNumber({billing_tx_charge_detail.v_duration})

then I can summarize @duration. What am I missing? It has to be something simple, but I'm just not seeing it. Is there a way to create a custom function to accomplish what I am trying to get here? Or is it even simpler than that?

One person suggested creating a SQL command in order to do the calculations before the data gets to the report. I am a SQL newb so I had no idea where to even begin with that.


Solution

  • If you are grouping by Service Code and placing the above formula in the footer you will only be computing {billing_tx_charge_detail.v_duration} for the last record in the group. If you are intending to use the formula and sum the results and place the results in the Service Code footer try the following. (basically remove the reference to group name)

    If {billing_tx_charge_detail.v_SERVICE_CODE}) = "1530" then 
    ToNumber({billing_tx_charge_detail.v_duration})/3 else
    ToNumber({billing_tx_charge_detail.v_duration})
    

    You can use variables (num1) if you want to but they are not needed. You can still use the second formula you referred to and place in the group footer OR you can place the first formula in details section, right click and insert a summary to the group footer. You can also place in the report footer if you need it to total there as well.