-- Create a window
CREATE WINDOW FilteredTradesWindow
SCHEMA (Symbol STRING, Price FLOAT,
Volume INTEGER)
KEEP 3 ROWS PER Symbol;
-- Insert rows into the window
INSERT INTO FilteredTradesWindow
SELECT *
FROM StockTrades;
-- Calculate an average and insert into the output stream
INSERT INTO FilteredTrades
SELECT *, AVG(Price)
FROM FilteredTradesWindow
WHERE Symbol = 'EBAY' OR Symbol = 'IBM'
GROUP BY Symbol;
The first two statements are exactly the same as before. The third statement sends rows to an output stream by connecting the output of one query to the input of another. Here's a brief explanation of the new code:
-
The new statement takes all of the columns from the row arriving from the input stream and the calculated average of the values in the Price column of the rows in the window (using one of the Sybase CEP predefined aggregate functions) and sends the resulting row to the output stream FilteredTrades.
-
The WHERE clause limits the output to rows with either "EBAY" or "IBM" in the Symbol column.
-
The GROUP BY clause causes the aggregate function to calculate the average for groups of rows in the window based on the value of the Symbol column. Without this clause, the aggregate function would perform the calculation over all rows in the window.
The output stream must have a new column to receive the average price.