CCL Function

Define a function using the DECLARE block.

The example creates a schema named TradeSchema, then uses the DECLARE block to declare the function MyWeightedAverage, which includes variables Value1 and Value2. The example also creates the local variable Weight1. A series of if and else if conditions determine the value of Weight1 based on whether Value2 is greater or less than the specified values. The resulting Weight1 value becomes a parameter in the to_money function.

DECLARE Money(2) MyWeightedAverage 
  (Money(2) Value1, Integer Value2)
{
	float Weight1 := 1.0;
	
    IF (Value2 > 10000 ) 
      { Weight1 := 0.5; }
    ELSE IF (Value2 > 4000) 
      {Weight1 := 0.75; }
    ELSE IF (Value2 < 100) 
      { Weight1 := 3.0; }
    ELSE IF (Value2 < 500) 
      { Weight1 := 0.25; }
    RETURN to_money(Value1 * Weight1 ,2);
}
end;

The example creates an input window named TradeWindow that references TradeSchema, and an output window named OutWeightedAverage that specifies an inline schema. OutWeightedAverage uses the MyWeightedAverage function within the avg() function.

CREATE  OUTPUT  WINDOW OutWeightedAverage
	SCHEMA ( Symbol String, avgPrice Money(2), wavgPrice Money(2))
	PRIMARY KEY deduced
	AS
	SELECT 
	    t.Symbol,
	    avg(t.Price) avgPrice,
	    avg(MyWeightedAverage(t.Price, t.Volume)) wavgPrice
	FROM 
	    TradeWindow t
	Group by t.Symbol
    ;

The example concludes by attaching a File CSV Input adapter named csvInConn1 to TradeWindow.