Average, Calculate, Count, Sum
These commands all do calculations with data from a table. AVERAGE, COUNT and SUM, as their names suggest, compute averages, count records and compute totals, respectively. CALCULATE can do all of that as well as compute minimum and maximum values, standard deviations and variances, and net present value.For all these commands, if TALK is ON, the number of records involved is displayed in the status bar, and for SUM, AVERAGE and CALCULATE, the result is displayed to the screen. If TALK is OFF, there's no visible display.
Usage |
AVERAGE [ nExpr1 [, nExpr2 [, ... ] ] ] [ Scope ] [ FOR lForCondition ] [ WHILE lWhileCondition ] [ TO nResult1 [, nResult2 [, ... ] ] | TO ARRAY Result ] [ NOOPTIMIZE ] COUNT [ Scope ] [ FOR lForCondition ] [ WHILE lWhileCondition ] [ TO nResult ] [ NOOPTIMIZE ] SUM [ nExpr1 [, nExpr2 [, ... ] ] ] [ Scope ] [ FOR lForCondition ] [ WHILE lWhileCondition ] [ TO nResult1 [, nResult2 [, ... ] ] | TO ARRAY Result ] [ NOOPTIMIZE ] |
Parameter |
Value |
Meaning |
nExpr1, nExpr2, ... |
Any numeric type |
The expression (involving one or more fields) to be summed or averaged. |
nResult1, nResult2, ... |
Numeric |
Variables to hold the averages, counts or sums. |
Result |
Numeric Array |
Array to hold the averages or sums. A one-dimensional array is created if it doesn't exist. The array is expanded if it's too small. |
Example |
USE Customer && TasTrade Customer table COUNT FOR Country = "UK" && number in the UK * See whether UK customers are big spenders AVERAGE Max_order_Amt FOR Country = "UK" USE Order_Line_Items ORDER Order_Id * Compute order total SEEK cOrderId && cOrderId holds an order number SUM quantity*unit_price WHILE order_id=cOrderId TO nOrderTotal |
Usage |
CALCULATE Func() [, Func() [ , ... ] ] [ Scope ] [ FOR lForCondition ] [ WHILE lWhileCondition ] [ TO eResult1 [, eResult2 [, ... ] ] | TO ARRAY Result ] [ IN cAlias | nWorkArea ] [ NOOPTIMIZE ] |
Parameter |
Value |
Meaning |
Func |
AVG( nExpr ) |
Computes the average of the non-null values of the enclosed numeric expression. |
CNT( [ nExpr ] ) |
Counts the number of records that match the conditions. If nExpr is specified, counts the number of records meeting the conditions for which nExpr isn't null. Note the use of nExpr here is undocumented. |
|
MAX( eExpr ) |
Finds the maximum of the non-null values for the specified expression. The expression may be Character, Date, Datetime or any numeric type. |
|
MIN( eExpr ) |
Finds the minimum of the non-null values for the specified expression. The expression may be Character, Date, Datetime or any numeric type. |
|
NPV( nExpr1, nExpr2 [ , nExpr3 ] ) |
Computes the net present value of a series of cash flows. See below for more explanation. |
|
STD( nExpr ) |
Computes the standard deviation of the non-null values of the enclosed numeric expression. |
|
SUM( nExpr ) |
Computes the total of the enclosed numeric expression. |
|
VAR( nExpr ) |
Computes the variance of the non-null values of the enclosed numeric expression. |
|
uResult1, uResult2, ... |
Same type as passed expression |
Variables to hold the results. |
Result |
Array |
Array to hold the results. A one-dimensional array is created if it doesn't exist. The array is expanded if it's too small. |
cAlias |
Character |
Perform the calculation on the table open with the specified alias. |
Omitted |
If nWorkArea is also omitted, perform the calculation on the table open in the current work area. |
|
nWorkArea |
Numeric |
Perform the calculation on the table open in the specified work area. |
Omitted |
If cAlias is also omitted, perform the calculation on the table open in the current work area. |
Example |
USE Products && from TasTrade * Display number, average unit price, * highest unit price, lowest unit price * for those that are available CALCULATE CNT(),AVG(Unit_Price), ; MIN(Unit_Price),MAX(Unit_Price) FOR ; NOT Discontinued |
See Also |