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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What are the different ways to do running totals?

Running Totals

What are the different ways to do running totals?

by  MalcolmW  Posted    (Edited  )
There are several ways to do a running total. Your options on which to use depend on your Seagate Crystal Reports (SCR) version number, and database type.

1) The easiest is to use the Insert|Running Total wizard. This is not always accurate in SCR v7 and below. Easy, but use at your own risk.

2) Use the three formula approach.
This is reliable, but more time consuming to write.
In this example, the running total calculates the number of employees in a dept.

//goes in group header for dept, resets count to zero
WhilePrintingRecords ;
NumberVar DeptEmpCount := 0

//goes in the detail section, increments employee count
WhilePrintingRecords ;
NumberVar DeptEmpCount := DeptEmpCount + 1

//goes in the group footer for dept, shows dept emp count
WhilePrintingRecords ;
NumberVar DeptEmpCount

3) Lastly, writing the RT in SQL will give you the best performance, but requires some experience with SQL. These running totals examples are for a bank account, where the running total is the balance of the account after each transaction.

SELECT B0.transaction, B0.transdate, SUM(B1.amount) AS balance
FROM BankAccount AS B0, BankAccount AS B1
WHERE B1.transdate <= B0.transdate

or using a scalar subquery (great terminology to confuse and intimidate)

SELECT B0.transaction, B0.transdate,
(SELECT SUM(B1.amount)
FROM BankAccount AS B1
WHERE B1.transdate <= B0.transdate) AS balance
FROM BankAccount AS B0

Crystal gurus will note that both of these SQL solutions require modification of the SELECT portion of the SQL statement, which isn't permitted in Crystal Reports. Thus, you will have to use a view, stored procedure, temporary table, or the SQL Query tool to circumvent this limitation.

Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top