Reference/es_mba_addformula
Syntax
int es_mba_addformula( struct es_mba *agg,
const char *formulaname,
const char *formuladefn,
int fldtype,
char *errbuf,
unsigned errbufsize );
Arguments
struct es_mba *agg 
Database aggregator. 
const char *formulaname 
Formula field name. 
const char *formuladefn 
Formula definition. 
int fldtype 
Formula field numerical type. See class field types for list of integer and doubleprecision types. 
char *errbuf 
Error message buffer. Populated by es_mba_addformula() on error. 
Return Codes
ES_ERROR_NONE 
Success. No errors. 
ES_ERROR_INVALIDFIELDTYPE 
ftype is not an integer or doubleprecision type. 

ES_ERROR_FIELDEXISTS 
forumulaname has already been specified as a formula. 

ES_ERROR_FAILTOPARSE 
Failed to parse formuladefn . errbuf is populated with an error message. 
Description
Create an algebraic formula to use in deriving key, result or other formula values.
formulaname can be regarded as the name of a variable that is assigned the result of evaluating the expression contained in the formuladefn parameter. The formula expression can refer to numerical constants, database fields and other formula names as variables. A set of mathematical operators and functions are provided. Usual mathematical operator precendence applies.
For example, a formula to compute aggregate profit from a sales database might look something like:
struct es_mba *agg;
char errbuf[1024];
es_mba_addformula( agg, "Profit", "DollarSales  UnitSales*(ProductCost+ShippingCost)",
ES_FLDTYPE_F64, errbuf, 1024 );
es_mba_addresult( agg, "Profit", ES_AGGTYPE_SUM );
An incomplete or otherwise invalid formuladefn causes es_mba_addformula() to fail with a ES_ERROR_FAILTOPARSE return code. errbuf is populated with an appropriate error message.
The set of valid formula constants, variables, operators and functions is as follows:
Constants 
42 
Integer numeric constant. 
42.0 
Doubleprecision numeric constant. 
Variables 
Profit 
Formula already defined via previous invocation of es_mba_addformula. 
UnitSales 
Database field defined in input database definition. Must be integer or doubleprecision. 
Numerical Operators 
() 
Parenthesis. 
+ 
Add. 
 
Subtract. 
* 
Multiply. 
/ 
Divide. 
Comparison Operators 
! 
Not (unary operator). 
== 
Equal. 
!= 
Not Equal. 
< 
Less than. 
<= 
Less than or equal. 
> 
Greater than. 
>= 
Greater than or equal. 
Logical Operators 
&& 
And. 
 
Or. 
Functions 
min(X,Y) 
Minimum. 
max(X,Y) 
Maximum. 
abs(X) 
Absolute. 
getyear(X) 
Get year from argument assumed to be a dateid. 
getmonth(X) 
Get month from argument assumed to be a dateid. 
getday(X) 
Get day from argument assumed to be a dateid. 
Type implication and coercion follows the C language convention: i.e. if both left and right arguments of a numerical or comparison operator is an integer, the operation will be fixedpoint. If either argument is a floatingpoint number, both arguments are coerced to floatingpoint and the subsequent operation will be floatingpoint. Comparison and logical operators both yield integer results.
The final result is coerced to the type defined by the fldtype parameter.
[Back To Reference]
[Back To Reference/es_mba]
