Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help: standard deviation formula

Status
Not open for further replies.

Igus

Technical User
Jan 30, 2001
9
IT
I have a table with the following fields:
1. order date
2. order number
3. code ordered
4. order amount
What I want is the average and the daily standard deviation for every different code.
I grouped data for {order date} and then for {code ordered}.
For the daily amount I did: sum ({order amount}, {order date}, “daily”)
I cannot calculate average and stddev with Crystal functions because it doesn’t consider the “null” daily amounts as zero (it doesn’t consider them at all). Could someone please tell me a std dev formula that can solve this problem? I tried a few different solutions, but I always get some kinda error.
 
If you know the generic formula for StdDev, I can show you how to build it in Crystal. Ken Hamady
Crystal Reports Training and a
Quick Reference Guide to VB/Crystal
 
The std dev formula is:
sqrt((n * sum[sq(x)-sq(sum(x))])/sq(n))
with:
sq = square
sqrt = squareroot
 
Which is your primary group, Day or Code?

It sounds like you did day as the first group, but I think you will need to do code first if you want the average days sales for each code, and the StDev of the daily sales number for each code.

It is the Summation part that is tricky. You will also need to create a running total using a variable to get around the need to do a sum of subtotals. Have you worked with Crystal variables?

Ken Hamady
Crystal Reports Training and a
Quick Reference Guide to VB/Crystal
 
Sorry, I guess I didn't explain well. I want the average amount for each code each day and the amount std dev (daily).Example:
1st january; order #1; code xxx; amount 100
1st january; order #2; code xxx; amount 200
3rd january; order #3; code xxx; amount 50
7th january; order #4; code xxx; amount 130

First 7 days of the year, amount average for code xxx is 480/7=68.57
stddev is:
SQ((100+200)- 68.57)= 53559.18
SQ(0 - 68.57)= 4702.04
SQ(50 - 68.57)= 344.89
SQ(0 - 68.57)= 4702.04
SQ(0 - 68.57)= 4702.04
SQ(0 - 68.57)= 4702.04
SQ(130 - 68.57)= 3773.46

sqrt ((53559.18 + 344.89 + 4702.04 + 4702.04 + 4702.04 + 4702.04 + 3773.46)/7)= 104.53

The problem is I need first group for days because I need an amount total for each day (1st jan amount is 100+200), then I need a second group for code.


 
Your example only shows one code, which makes me think that you are doing each code separately. That would mean that each day is a group within a larger group for this code - XXX.

If this is not the case, show an example for 2 products so I can see the relationship between code and day groups. Ken Hamady
Crystal Reports Training and a
Quick Reference Guide to VB/Crystal
 
The same order (same order#) may have different codes with different amounts
Here is the example:
1st january; order #1; code xxx; amount 100
1st january; order #1; code yyy; amount 100
1st january; order #2; code xxx; amount 200
3rd january; order #3; code xxx; amount 50
5th january; order #4; code yyy; amount 75
7th january; order #5; code xxx; amount 130
7th january; order #7; code yyy; amount 230

First 7 days of the year, amount average for code xxx is 480/7=68.57
stddev is:
SQ((100+200)- 68.57)= 53559.18
SQ(0 - 68.57)= 4702.04
SQ(50 - 68.57)= 344.89
SQ(0 - 68.57)= 4702.04
SQ(0 - 68.57)= 4702.04
SQ(0 - 68.57)= 4702.04
SQ(130 - 68.57)= 3773.46
sqrt ((53559.18 + 344.89 + 4702.04 + 4702.04 + 4702.04 + 4702.04 + 3773.46)/7)= 104.53

First 7 days of the year, amount average for code yyy is 405/7=57.857
stddev is:
SQ(100 - 57.857)= 1776.03
SQ(0 - 57.857)= 3347.43
SQ(0 - 57.857)= 3347.43
SQ(0 - 57.857)= 3347.43
SQ(75 - 57.857)= 293.88
SQ(0 - 57.857)= 3347.43
SQ(230 - 57.857)= 29633.21
sqrt ((1776.03 + 3347.43 + 3347.43 + 3347.43 + 293.88 + 3347.43 + 29633.21)/7)= 80.26
 
Your example is what I was describing. Calculating the StDev of each code accross several days, one code at a time.
I can show you how to calculate each code's StDev across days only if the report is grouped by code first, and then by day within each code. That is how you calculated your last example, one code at a time. Even if Crystal's default StDev function didn't skip blank days, you would also have to group by code first to get the STD of each code group accross several days.

Otherwise you will be trying to calculate several StdDevs simultaneously.

Can you group by Code then Day?
Being on the same order seems irrelevant.
Am I missing something here? Ken Hamady
Crystal Reports Training and a
Quick Reference Guide to VB/Crystal
 
I'm outside office today so I cannot check, but it seems you are right and I'm stupid :)
It happens.
What shall I do after grouping?
 
You have two groups (code and day)
Subtotal the amount by both code and day.
Take your end date, subtract your start date, and that is the count of days.
Divide the sum of amount for the whole Code and by the DayCount and you get your average for that code.

Now you take each day subtotal and subtract the average and square the result:
(Sum ({Amount}, {Day}) - {@Avg})^2

Now you create a running total to add up these squared values once per day. This is where you need Crystal variables, which is the only way to add up totals or fields that are made of totals. See the FAQ on running totals and use the three formula technique. it will look something like this:

WhilePrintingRecords;
NumberVar SumSq;
SumSq:= SumSq+{@Square}

The key is to put this formula on the group footer for the day, so that it only picks up one value per day.

Now you have to add the squares of the empty days and calculate the StdDev. You do this by counting the number of different days in the code group, and subtracting this from the daycount you calculated above. Since all of these days are zero they all have the same square value:

WhilePrintingRecords;
NumberVar SumSq;
NumberVar Extras;

Extras := ({@daycount} - DistinctCount ({days},{code})) * (0-{@Avg})^2;

sqr((SumSq+Extras)/{DayCount}-1) Ken Hamady
Crystal Reports Training and a
Quick Reference Guide to VB/Crystal
 
Thank you very much. Everything is working fine. As I told you I tried a few different solutions and one of them was very similar to the right one, but I didn't get the problem with running totals. Thank you again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top