Symptom
How to calculate the sum of Top N records Crystal Designer
Environment
Crystal Reports 2008 (All Versions)
Resolution
1. First off all, sort the records in descending order based on the database field, would like to summarize. For eg: if the report shows top 5 customers in US based on sales value, then sort the report on sales value in Descending order.
2. Create a formula using the following code:
WhilePrintingRecords;
numbervar inc;
numbervar top_n;
inc := inc + 1;
if inc <= 5 then //where 5 means top 5
top_n := top_n + {field_to_summarize}
3. Place the above formula on the Details section.
4. Create another formula to display the top n sums:
whileprintingrecords
numbervar top_n;
5. Place this formula on the Report/Page Footer.
6. If the report is already grouped on a database field, then create a formula to reset the variables as follows:
whileprintingrecords
numbervar top_n := 0;
numbervar inc := 0;
7. Place the above formula in the Group Header.
8. You may then place the formula that displays the sum in the Group Footer.
Keywords
Sum of top n, sum, top n , KBA , BI-RA-CR-DSL , Crystal Reports on Data Semantic Layer , Problem
SAP Knowledge Base Article - Public