Creating an Aggregate as a Simple Query

Add to the sample diagram an Aggregate simple query to create a volume weighted average price (VWAP).

An Aggregate query groups events that have common values, and computes summary statistics for the group.

  1. In the Visual editor Palette, in Streams and Windows, click Aggregate (aggregate_template.gif).
  2. Click in the diagram to create the object.
  3. Change the default name, Aggregate1, to VWAP.
  4. Connect PriceFeed to the VWAP aggregate:
    1. Click the Connector shape in the Palette.
    2. Click the PriceFeed input window, then click the VWAP aggregate.
      The aggregate border changes from red to black, indicating that it is valid, now that it has input.
  5. Enter Column Expressions:
    1. Click Copy Columns from Input (copycolumn.gif) in the shape toolbar to select the columns to copy into the schema for the aggregate window.
    2. Add additional columns by clicking Add Column Expressions compute_template.gif in the shape toolbar.
    For this example, copy these columns:
    • PriceFeed.Symbol
    • PriceFeed.Price
    • PriceFeed.TradeTime

    Then add another column and edit its name to VWAP.

  6. Edit column expressions by double-clicking to open the inline editor, or by selecting the expressions and pressing Ctrl+F2 to open the expression in the pop-up editor.
    For this example, make these changes:
    • Edit Price to LastPrice
    • Edit TradeTime to LastTime
    • Edit the VWAP column expression to:
    ( sum ( ( PriceFeed.Price * 
      CAST ( FLOAT , PriceFeed.Shares ) ) ) / 
      CAST (FLOAT , sum ( PriceFeed.Shares ) ) )
  7. Click Add GroupBy Clause ({ }) to edit the grouping of columns in the aggregate object.
    Note: The Aggregate shape must have exactly one GROUP BY expression.
    For this example, select PriceFeed.Symbol as the grouping column.
  8. (Optional) Click Set Keep Policy retention_window.gif to create a retention window.
    The default policy is to keep all rows of incoming data. You can also choose to keep only the last row, a specific number of rows, or keep the rows for a specific time. This defines the CCL KEEP clause.
  9. (Optional) Use the Toggle window.gif option to change the aggregate object from LOCAL to OUTPUT.