xHarbour Reference Documentation > Command Reference xHarbour Developers Network  

SUM

Calculates the sum of numeric expressions in the current work area.

Syntax

   SUM <expressions,...> ;
    TO <resultVarNames,...> ;
      [<Scope>] ;
[WHILE <lWhileCondition>] ;
  [FOR <lForCondition>]

Arguments

<expressions,...>
This is a comma separated list of numeric expressions that are evaluated for the records of the current work area.
TO <resultVarNames,...>
A comma separated list of variable names that are assigned the results of the calculation. The number of <resultVarNames,...> must match exactly the number of <expressions,...>.
<Scope>
This option defines the number of records to sum. It defaults to ALL. The NEXT <nCount> sums the next <nCount> records, while the REST scope sums records beginning with the current record down to the end of file.
WHILE <lWhileCondition>
This is a logical expression indicating to continue calculation while the condition is true. The SUM operation stops as soon as <lWhileCondition> yields .F. (false).
FOR <lForCondition>
This is a logical expression which is evaluated for all records in the current work area. Those records where <lForCondition> yields .T. (true) are included in the calculation.

Description

The SUM command is used to calculate sum values for one or more numeric expressions. The expressions are evaluated in the current work area. The number of records to include in the calculation can be restricted using a FOR and/or WHILE condition or by explicitly defining a scope.

Records marked for deletion are not included in the calculation when SET DELETED is set to ON.

Info

See also:AVERAGE, DbEval(), TOTAL
Category: Database commands
Source:rdd\dbcmd.c
LIB:xhb.lib
DLL:xhbdll.dll

Example

// The example adds daily sales for the month of December.

   PROCEDURE Main
      LOCAL nSales := 0

      USE Invoice

      SUM FIELD->Total TO nSales FOR ;
          Month(PayDate) == 12 .AND. Year(PayDate)=Year(Date())

      ? "December sales:", nSales

      CLOSE Invoice
   RETURN

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe