Search code examples
crystal-reportssage-crm

Create a chart using the records of certain type grouped by month, with a moving balance


I am trying to create a chart (bar or line) in crystal from one table in my database (Sage CRM).

The records are as follows

CustomerId      Date            Invoice              Amount

1234           3/4/2013        Cust Invoice           3322.00   
1234           3/4/2013        Payment                2445.00  
1234           4/5/2013        A/c transaction         322.00  
1234           5/6/2013        interest                 32.00 
1234           6/6/2013        payment                 643.00 

So I would like to have a report that meets the following criteria

  1. Only records for the last 12 months grouped in month
  2. Only invoice types of payment, invoice and interest
  3. A moving balance that calculates all the invoice amounts ie

(when displaying the information for July 2012, the moving balance will be the total of all invoices prior to this date. Without this field I can create the chart no problem using select expert but I am not sure now what to do)

Should I use a cross tab? if so how will I do the selection to only show the invoices I want and the the date range I want?


Solution

  • After spending almost a week on this problem, with a lot of help from an expert I have finally got a solution.

    In order to create a amount that is the sum of all records for a company, month and invoice type since the beginning of time, while only displaying records for the last year, I have created a SQL command

    Select 
      //All of the fields for the report,
      movingBalance.Amount
    from myInvoiceTable as mit
      <join to any other tables for the report>
      left join (
        select customerID, sum(amount) as Amount
        from myInvoiceTable
        where Record_Type in ('Payment', 'Invoice','Interest')
          and Date < {?Report Start Date}
        group by customerID) movingBalance
       on mit.customerID = movingBalance.customerID
    where mit.RecordType in ('Payment', 'Invoice','Interest')
      and mit.Date >= {?Report Start Date}
      and mit.Date <= {?Report End Date}
    

    There are a couple of tricks to using commands:

    1. For performance reasons you generally want to include ALL of the data for the report in a single command. Avoid joining multiple commands or joining one or more tables to a command.
    2. Filter the data in the command, NOT in the Select Expert in the report.
    3. Create any parameters in the Command Editor, not in the main report. Parameters created in the report won't work in the Command Editor.

    This has done the trick.