Consolidating the Code

Create window statement and a single query statement.

At this point, the query module uses one Create Window statement and two Query statements. You can achieve the same result with a single Query statement.

  1. Replace the last three statements in the Queries tab, beginning with "-- Create a window", with the following text (or copy the text from MaintainingStateCCL.txt between the lines "-- Maintaining State Code Block 3" and "-- Maintaining State Code Block 4"):
    -- Create an unnamed window to track average price by stock symbol
    INSERT INTO FilteredTrades
    SELECT *, AVG(Price)
    FROM StockTrades
    KEEP 3 ROWS
    WHERE Symbol = 'EBAY' OR Symbol = 'IBM'
    GROUP BY Symbol;
    

    This single statement produces the same results as the three statements introduced in Aggregating Values. In this case, the KEEP clause by itself creates an implicit or unnamed window (as opposed to the named window you defined before). When you use the GROUP BY clause with an unnamed window, it groups the aggregate calculations and also groups the contents of the window, just like the PER clause. In this case, the window behaves as if you had specified "KEEP 3 ROWS PER Symbol".

  2. Run the project and verify that the results are as expected.

    Note that the WHERE clause in this query restricts the output and aggregate calculation, not the contents of the unnamed window - the window holds three rows for every value of the Symbol column, not just for EBAY and IBM.

    You cannot view unnamed windows with Sybase CEP Studio.

  3. Stop the project.