EPL Reference: Clauses 2-21
The RSTREAM keyword is used to indicate to the engine to generate only remove stream events. This can be useful if we want to trigger actions when events leave a
window rather then when events enter a window. The statement below generates CombinedEvent events when EventA and EventB leave the window after 30
minutes.
INSERT RSTREAM INTO CombinedEvent SELECT A.customerId AS custId, A.timestamp - B.timestamp AS latency
FROM EventA A, EventB B RETAIN 30 MIN WHERE A.txnId = B.txnId
2.12 Simple and Correlated Subqueries
A subquery is a SELECT within another statement. EPL supports subqueries in the SELECT clause and in the WHERE clause of EPL statements. Subqueries provide an
alternative way to perform operations that would otherwise require complex joins. Subqueries can also make statements more readable than complex joins.
EPL supports both simple subqueries as well as correlated subqueries. In a simple subquery, the inner query does not reference any elements rows from the outer
query. The following example shows a simple subquery within a SELECT clause:
SELECT assetId, SELECT zone
FROM ZoneClosed.std:lastevent AS lastClosed FROM RFIDEvent
SELECT assetId, SELECT zone
FROM ZoneClosed RETAIN ALL EVENTS AS lastClosed FROM RFIDEvent
If the inner query is dependent on the outer query, it is referred to as a correlated subquery, as shown in the following example. In the query, the WHERE clause in the
inner query involves a stream from the outer query:
SELECT FROM RfidEvent AS RFID
WHERE Dock 1 = SELECT name
FROM Zones RETAIN ALL EVENTS WITH UNIQUE zoneId WHERE zoneId = RFID.zoneId
The preceding example shows a subquery in the WHERE clause. The statement selects RFID events in which the zone name matches a string constant based on zone ID. The
statement uses the WITH UNIQUE subclause in the RETAIN clause to guarantee that only the last event per zone ID is held from processing by the subquery.
The following example is a correlated subquery within a SELECT clause. In this query, the SELECT clause retrieves the zone name by means of a subquery against the Zones
set of events correlated by zone id:
SELECT zoneId, SELECT name
FROM Zones RETAIN ALL EVENTS WITH UNIQUE zoneId WHERE zoneId = RFID.zoneId AS name
FROM RFIDEvent
2-22 Oracle Complex Event Processing EPL Language Reference
When a simple or correlated subquery returns multiple rows, Oracle CEP returns a null value as the subquery result. To limit the number of events returned by a
subquery, consider using WITH UNIQUE or PARTITION BY in the RETAIN clause.
The SELECT clause of a subquery also allows wildcard selects, which return as an event property the underlying event object of the event type as defined in the FROM
clause. An example:
SELECT SELECT
FROM MarketData RETAIN 1 EVENT AS md MATCHING WITHIN 10 SECONDS
The output events of the preceding statement contain the underlying MarketData event in a property named md. The statement populates the last MarketData event
into a property named md every 10 seconds following the pattern definition, or populates a null value if no MarketData event has been encountered so far.
The following restrictions apply to subqueries:
■
The subquery stream definition must define a data window or other view to limit subquery results, reducing the number of events held for subquery execution.
■
You cannot use aggregation functions in subqueries. Instead, use the INSERT into clause to provide aggregation results for use in subqueries
■
Subqueries can consist only of a SELECT clause, a FROM clause, and a WHERE clause. The GROUP BY and HAVING clauses, as well as joins, outer-joins and
output rate limiting are not permitted within subqueries.
The performance of your statement that contains one or more subqueries principally depends on two parameters. First, if your subquery correlates one or more columns in
the subquery stream with the enclosing statements streams using equals =, Oracle CEP automatically builds the appropriate indexes for fast row retrieval based on the
key values correlated joined. The second parameter is the number of rows found in the subquery stream and the complexity of the filter criteria WHERE clause, as each
row in the subquery stream must evaluate against the WHERE clause filter.
2.13 Parameterized Queries