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!

totalling many fields 2

Status
Not open for further replies.

Phil Thoms

Programmer
Oct 31, 2005
245
GB
Hello,
I have a database file with just a few fields that require totalling into a totals field eg 'replace totals with field1+field2+field3' etc. there are only five fields to be totalled at the moment but this may increase to at least fifteen.
Is there a betted method of totalling many fields than the long winded way with the replace command?
Thanks
 
What's longwinded with the replace command?

Besides a normal table layout would not total 15 fields of a record into 1 total field but would have one numeric field to take a sum by aggregation eg SUM(field). And you wouldn't store that total, as it's a computed value, so it would be storing redundant data. The only good redundancy in data is backups.

Bye, Olaf.
 
I suppose if you really wanted to, you could do it in a loop, something like this:

Code:
SELECT MyTable
lnTotal = 0
FOR lnI = 1 TO 15
  lnTotal = lnTotal + EVALUATE(FIELD(lnI))
ENDFOR 
REPLACE TotalField WITH lnTotal

But is it worth it? Unless you will be writing this code many times, you might as well stick to your original plan:

Code:
REPLACE Totals WITH Field1 + Field2 + Field3 + ... etc.

Do also take note of Olaf's point about redundancy. There's little pointing in storing a total when it's trivially easy to calculate it as it's needed.

Mike




__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top