An expression is a combination of one or more values and one or more operations. A value can be a constant having a definite value, a function that evaluates to a value, or an attribute containing a value. Every expression maps to a datatype.
This simple expression evaluates to 4 and has datatype NUMBER (the same datatype as its components):
2*2
The following expression is an example of a more complex expression that uses both functions and operators. The expression adds seven days to the current date, removes the time component from the sum, and converts the result to CHAR datatype:
TO_CHAR(TRUNC(SYSDATE+7))
Oracle CEP provides the following expressions:
Aggregate distinct expressions: "aggr_distinct_expr".
Aggregate expressions: "aggr_expr".
Arithmetic expressions: "arith_expr".
Arithmetic expression list: "arith_expr_list".
Case expressions: "case_expr".
Decode expressions: "decode".
Function expressions: "func_expr".
Order expressions: "order_expr".
XML aggregate expressions: "xml_agg_expr"
XML column attribute value expressions: "xmlcolattval_expr".
XML element expressions: "xmlelement_expr".
XML forest expressions: "xmlforest_expr".
XML parse expressions: "xml_parse_expr"
You can use expressions in:
The select list of the SELECT statement
A condition of the WHERE clause and HAVING clause
The VALUES clause of the INSERT statement
Oracle CEP does not accept all forms of expressions in all parts of all Oracle CQL statements. Refer to the individual Oracle CQL statements in Chapter 16, "Oracle CQL Statements" for information on restrictions on the expressions in that statement.
You must use appropriate expression notation whenever expr appears in conditions, Oracle CQL functions, or Oracle CQL statements in other parts of this reference. The sections that follow describe and provide examples of the various forms of expressions.
Note:
In stream input examples, lines beginning withh (such as h 3800) are heartbeat input tuples. These inform Oracle CEP that no further input will have a timestamp lesser than the heartbeat value.Use an aggr_distinct_expr aggregate expression when you want to use an aggregate built-in function with distinct. When you want to use an aggregate built-in function without distinct, see "aggr_expr".

You can specify an arith_distinct_expr as the argument of an aggregate expression.
You can use an aggr_distinct_expr in the following Oracle CQL statements:
For more information, see Chapter 6, "Functions: Aggregate".
Example 11-2 shows how to use a COUNT aggregate distinct expression.
Example 11-1 aggr_distinct_expr for COUNT
create view viewq2Cond1(ACCT_INTRL_ID, sumForeign, countForeign) as
select ACCT_INTRL_ID, sum(TRXN_BASE_AM), count(distinct ADDR_CNTRY_CD)
from ValidCashForeignTxn[range 48 hours]
group by ACCT_INTRL_ID
having ((sum(TRXN_BASE_AM) * 100) >= (1000 * 60) and
(count(distinct ADDR_CNTRY_CD >= 2)))
Use an aggr_expr aggregate expression when you want to use aggregate built-in functions. When you want to use an aggregate built-in function with distinct, see "aggr_distinct_expr"

(arith_expr::=, identifier::=, xml_agg_expr::=)
You can specify an arith_expr as the argument of an aggregate expression.
The first and last aggregate built-in functions take a single argument made up of the following period separated values:
identifier1: the name of a pattern as specified in a DEFINE clause.
identifier2: the name of a stream element as specified in a CREATE STREAM statement.
You can use an aggr_expr in the following Oracle CQL statements:
For more information, see:
Example 11-2 shows how to use a COUNT aggregate expression.
Use an arith_expr arithmetic expression to define an arithmetic expression using any combination of stream element attribute values, constant values, the results of a function expression, aggregate built-in function, case expression, or decode. You can use all of the normal arithmetic operators (+,-,*, and /) and the concatenate operator (||).

(attr::=, const_value::=, func_expr::=, aggr_expr::=, aggr_distinct_expr::=, case_expr::=, decode::=, arith_expr::=)
You can use an arith_expr in the following Oracle CQL statements:
For more information, see "Arithmetic Operators".
Example 11-2 shows how to use a arith_expr expression.
Use an arith_expr_list arithmetic expression list to define one or more arithmetic expressions using any combination of stream element attribute values, constant values, the results of a function expression, aggregate built-in function, case expression, or decode. You can use all of the normal arithmetic operators (+,-,*, and /) and the concatenate operator (||).
You can use an arith_expr_list in the following Oracle CQL statements:
For more information, see "Arithmetic Operators".
Example 11-4 shows how to use a arith_expr_list expression.
Use a case_expr case expression to evaluate stream elements against multiple conditions.

(searched_case_list::=, arith_expr::=, simple_case_list::=)
(non_mt_cond_list::=, arith_expr::=)
The case_expr is similar to the DECODE clause of an arithmetic expression (see "decode").
In a searched_case clause, when the non_mt_cond_list evaluates to true, the searched_case clause may return either an arithmetic expression or null.
In a simple_case clause, when the arithmetic expression is true, the simple_case clause may return either another arithmetic expression or null.
You can use an case_expr in the following Oracle CQL statements:
This section describes the following case_expr examples:
Consider the query q97 in Example 11-5 and the data stream S0 in Example 11-6. Stream S1 has schema (c1 float, c2 integer). The query returns the relation in Example 11-7.
Example 11-5 CASE Expression: SELECT * Query
<query id="q97"><![CDATA[
select * from S0
where
case
when c2 < 25 then c2+5
when c2 > 25 then c2+10
end > 25
]]></query>
Example 11-6 CASE Expression: SELECT * Stream Input
Timestamp Tuple
1000 0.1,10
1002 0.14,15
200000 0.2,20
400000 0.3,30
500000 0.3,35
600000 ,35
h 800000
100000000 4.04,40
h 200000000
Example 11-7 CASE Expression: SELECT * Relation Output
Timestamp Tuple Kind Tuple 400000:+ 0.3,30 500000:+ 0.3,35 600000:+ ,35 100000000:+ 4.04,40
Consider the query q96 in Example 11-8 and the data streams S0 in Example 11-9 and S1 in Example 11-10. Stream S0 has schema (c1 float, c2 integer) and stream S1 has schema (c1 float, c2 integer). The query returns the relation in Example 11-11.
Example 11-8 CASE Expression: SELECT Query
<query id="q96"><![CDATA[
select
case to_float(S0.c2+10)
when (S1.c2*100)+10 then S0.c1+0.5
when (S1.c2*100)+11 then S0.c1
else S0.c1+0.3
end
from
S0[rows 100],
S1[rows 100]
]]></query>
Example 11-9 CASE Expression: SELECT Stream S0 Input
Timestamp Tuple
1000 0.1,10
1002 0.14,15
200000 0.2,20
400000 0.3,30
500000 0.3,35
600000 ,35
h 800000
100000000 4.04,40
h 200000000
Example 11-10 CASE Expression: SELECT Stream S1 Input
Timestamp Tuple
1000 10,0.1
1002 15,0.14
200000 20,0.2
300000 ,0.2
400000 30,0.3
100000000 40,4.04
Example 11-11 CASE Expression: SELECT Relation Output
Timestamp Tuple Kind Tuple
1000: + 0.6
1002: + 0.44
1002: + 0.4
1002: + 0.14
200000: + 0.5
200000: + 0.5
200000: + 0.4
200000: + 0.44
200000: + 0.7
300000: + 0.4
300000: + 0.44
300000: + 0.7
400000: + 0.6
400000: + 0.6
400000: + 0.6
400000: + 0.6
400000: + 0.4
400000: + 0.44
400000: + 0.5
400000: + 0.8
500000: + 0.6
500000: + 0.6
500000: + 0.6
500000: + 0.6
500000: + 0.6
600000: +
600000: +
600000: +
600000: +
600000: +
100000000: + 4.34
100000000: + 4.34
100000000: + 4.34
100000000: + 4.34
100000000: + 4.34
100000000: + 0.4
100000000: + 0.44
100000000: + 0.5
100000000: + 0.6
100000000: + 0.6
100000000: +
100000000: + 4.34
Use a decode expression to evaluate stream elements against multiple conditions.
DECODE expects its non_mt_arg_list to be of the form:
expr, search1, result1, search2, result2, ... , searchN, result N, default
DECODE compares expr to each search value one by one. If expr equals a search value, the DECODE expressions returns the corresponding result. If no match is found, the DECODE expressions returns default. If default is omitted, the DECODE expressions returns null.
The arguments can be any of the numeric (INTEGER, BIGINT, FLOAT, or DOUBLE) or character (CHAR) datatypes. For more information, see Section 2.2, "Datatypes").
The search, result, and default values can be derived from expressions. Oracle CEP uses short-circuit evaluation. It evaluates each search value only before comparing it to expr, rather than evaluating all search values before comparing any of them with expr. Consequently, Oracle CEP never evaluates a search i, if a previous search j (0 < j < i) equals expr.
Oracle CEP automatically converts expr and each search value to the datatype of the first search value before comparing. Oracle CEP automatically converts the return value to the same datatype as the first result.
In a DECODE expression, Oracle CEP considers two nulls to be equivalent. If expr is null, then Oracle CEP returns the result of the first search that is also null.
The maximum number of components in the DECODE expression, including expr, searches, results, and default, is 255.
The decode expression is similar to the case_expr (see case_expr::=).
You can use a decode expression in the following Oracle CQL statements:
Consider the query q in Example 11-12 and the input relation R in Example 11-13. Relation R has schema (c1 float, c2 integer). The query returns the relation in Example 11-14.
Example 11-12 Arithmetic Expression and DECODE Query
<query id="q"><![CDATA[
...
SELECT DECODE (c2, 10, c1+0.5, 20, c1+0.1, 30, c1+0.2, c1+0.3) from R
]]></query>
Example 11-13 Arithmetic Expression and DECODE Relation Input
Timestamp Tuple Kind Tuple
1000: + 0.1,10
1002: + 0.14,15
2000: - 0.1,10
2002: - 0.14,15
200000: + 0.2,20
201000: - 0.2,20
400000: + 0.3,30
401000: - 0.3,30
500000: + 0.3,35
501000: - 0.3,35
600000: + ,35
601000: - ,35
100000000: + 4.04,40
100001000: - 4.04,40
Use the func_expr function expression to define a function invocation using any Oracle CQL built-in or user-defined function.

(identifier::=, const_int::=, const_bigint::=, const_string::=, xqryargs_list::=, non_mt_arg_list::=, xml_parse_expr::=, xmlelement_expr::=, xmlforest_expr::=, xmlcolattval_expr::=, arith_expr::=)
The PREV function takes a single argument made up of the following period-separated identifier arguments:
identifier1: the name of a pattern as specified in a DEFINE clause.
identifier2: the name of a stream element as specified in a CREATE STREAM statement.
The PREV function also takes the following const_int arguments:
const_int: the index of the stream element before the current stream element to compare against. Default: 1.
const_bigint: the timestamp of the stream element before the current stream element to compare against. To obtain the timestamp of a stream element, you can use the ELEMENT_TIME pseudocolumn (see Section 3.2, "ELEMENT_TIME Pseudocolumn").
For more information, see "prev". For an example, see "func_expr PREV Function Example".
XQuery: XMLEXISTS and XMLQUERY
You can specify an XQuery that Oracle CEP applies to the XML stream element data that you bind in xqryargs_list. For more information, see:
An xqryargs_list is a comma separated list of one or more xqryarg instances made up of an arithmetic expression involving one or more stream elements from the select list, the AS keyword, and a const_string that represents the XQuery variable or operator (such as the "." current node operator). For more information, see xqryargs_list::=.
For an example, see "func_expr XMLQUERY Function Example".
The XMLCONCAT function returns the concatenation of its comma-delimited xmltype arguments as an xmltype.
For more information, see "xmlconcat".
The SQLX specification extends SQL to support XML data. Oracle CQL provides the following expressions (and functions) to manipulate data from an SQLX stream. For example, you can construct XML elements or attributes with SQLX stream elements, combine XML fragments into larger ones, and parse input into XML content or documents.
For more information on Oracle CQL support for SQLX, see:
For more information on datatype restrictions when using Oracle CQL with XML, see:
For more information on SQLX, see the Oracle Database SQL Language Reference.
The FIRST and LAST functions each take a single argument made up of the following period-separated values:
identifier1: the name of a pattern as specified in a DEFINE clause.
identifier2: the name of a stream element as specified in a CREATE STREAM statement.
For more information, see:
You can specify the identifier of a function explicitly with or without a non_mt_arg_list: a list of arguments appropriate for the built-in or user-defined function being invoked. The list can have single or multiple arguments.
You can specify the identifier of a function explicitly with a distinct arithmetic expression. For more information, see aggr_distinct_expr.
You can use a func_expr in the following Oracle CQL statements:
For more information, see Section 1.1.9, "Functions".
This section describes the following func_expr examples:
func_expr PREV Function Example
Example 11-15 shows how to compose a func_expr to invoke the PREV function.
Example 11-15 func_expr for PREV
<query id="q36"><![CDATA[
select T.Ac1 from S15
MATCH_RECOGNIZE (
PARTITION BY
c2
MEASURES
A.c1 as Ac1
PATTERN(A)
DEFINE
A as (A.c1 = PREV(A.c1,3,5000) )
) as T
]]></query>
func_expr XMLQUERY Function Example
Example 11-16 shows how to compose a func_expr to invoke the XMLQUERY function.
Example 11-16 func_expr for XMLQUERY
<query id="q1"><![CDATA[
select
xmlexists(
"for $i in /PDRecord where $i/PDId <= $x return $i/PDName"
passing by value
c2 as ".",
(c1+1) as "x"
returning content
) xmldata
from
S1
]]></query>
Example 11-17 shows how to compose a func_expr to invoke the SUM function.
Use the order_expr expression to specify the sort order in which Oracle CEP returns tuples that a query selects.

You can specify a stream element by attr name.
Alternatively, you can specify a stream element by its const_int index where the index corresponds to the stream element position you specify at the time you register or create the stream.
You can use an order_expr in the following Oracle CQL statements:
Consider Example 11-18. Stream S3 has schema (c1 bigint, c2 interval, c3 byte(10), c4 float). This example shows how to order the results of query q210 by c1 and then c2 and how to order the results of query q211 by c2, then by the stream element at index 3 (c3) and then by the stream element at index 4 (c4).
Use an xml_agg_expr expression to return a collection of XML fragments as an aggregated XML document. Arguments that return null are dropped from the result.
(arith_expr, order_by_clause::=)
You can specify an xml_agg_expr as the argument of an aggregate expression.
You can use an xml_agg_expr in the following Oracle CQL statements:
For more information, see:
Consider the query tkdata67_q1 in Example 11-19 and the input relation tkdata67_S0 in Example 11-20. Relation tkdata67_S0 has schema (c1 integer, c2 float). The query returns the relation in Example 11-21.
Example 11-19 xml_agg_expr Query
<query id="tkdata67_q1"><![CDATA[
select
c1,
xmlagg(xmlelement("c2",c2))
from
tkdata67_S0[rows 10]
group by c1
]]></query>
Use an xmlcolattval_expr expression to create an XML fragment and then expand the resulting XML so that each XML fragment has the name column with the attribute name.
You can specify an xmlcolattval_expr as the argument of a function expression. It is especially useful when processing SQLX streams. For more information, see "SQL/XML (SQLX)".
You can use an xmlcolattval_expr in the following Oracle CQL statements:
For more information, see "XMLCOLATTVAL" in the Oracle Database SQL Language Reference.
Consider the query tkdata53_q1 in Example 11-19 and the input relation tkdata53_S0 in Example 11-20. Relation tkdata53_S0 has schema (c1 integer, c2 float). The query returns the relation in Example 11-21.
Example 11-22 xmlcolattval_expr Query
<query id="tkdata53_q1"><![CDATA[
select
XMLELEMENT("tkdata53_S0", XMLCOLATTVAL( tkdata53_S0.c1, tkdata53_S0.c2))
from
tkdata53_S0 [range 1]
]]></query>
Example 11-23 xmlcolattval_expr Relation Input
Timestamp Tuple
1000: 10, 0.1
1002: 15, 0.14
200000: 20, 0.2
400000: 30, 0.3
h 800000
100000000: 40, 4.04
h 200000000
Example 11-24 xmlcolattval_expr Relation Output
Timestamp Tuple Kind Tuple
1000: + <tkdata53_S0>
<column name="c1">10</column>
<column name="c2">0.1</column>
</tkdata53_S0>
1002: + <tkdata53_S0>
<column name="c1">15</column>
<column name="c2">0.14</column>
</tkdata53_S0>
2000: - <tkdata53_S0>
<column name="c1">10</column>
<column name="c2">0.1</column>
</tkdata53_S0>
2002: - <tkdata53_S0>
<column name="c1">15</column>
<column name="c2">0.14</column>
</tkdata53_S0>
200000: + <tkdata53_S0>
<column name="c1">20</column>
<column name="c2">0.2</column>
</tkdata53_S0>
201000: - <tkdata53_S0>
<column name="c1">20</column>
<column name="c2">0.2</column>
</tkdata53_S0>
400000: + <tkdata53_S0>
<column name="c1">30</column>
<column name="c2">0.3</column>
</tkdata53_S0>
401000: - <tkdata53_S0>
<column name="c1">30</column>
<column name="c2">0.3</column>
</tkdata53_S0>
100000000: + <tkdata53_S0>
<column name="c1">40</column>
<column name="c2">4.04</column>
</tkdata53_S0>
100001000: - <tkdata53_S0>
<column name="c1">40</column>
<column name="c2">4.04</column>
</tkdata53_S0>
Use an xmlelement_expr expression when you want to construct a well-formed XML element from stream elements.

(const_string::=, arith_expr::=, xml_attribute_list::=, arith_expr_list::=)
You can specify an xmlelement_expr as the argument of a function expression. It is especially useful when processing SQLX streams. For more information, see "SQL/XML (SQLX)".
You can use an xmlelement_expr in the following Oracle CQL statements:
For more information, see "XMLELEMENT" in the Oracle Database SQL Language Reference.
Consider the query tkdata51_q0 in Example 11-25 and the input relation tkdata51_S0 in Example 11-26. Relation tkdata51_S0 has schema (c1 integer, c2 float). The query returns the relation in Example 11-27.
Example 11-25 xmlelement_expr Query
<query id="tkdata51_q0"><![CDATA[
select
XMLELEMENT(
NAME "S0",
XMLELEMENT(NAME "c1", tkdata51_S0.c1),
XMLELEMENT(NAME "c2", tkdata51_S0.c2)
)
from
tkdata51_S0 [range 1]
]]></query>
Example 11-26 xmlelement_expr Relation Input
Timestamp Tuple
1000: 10, 0.1
1002: 15, 0.14
200000: 20, 0.2
400000: 30, 0.3
h 800000
100000000: 40, 4.04
h 200000000
Example 11-27 xmlelement_expr Relation Output
Timestamp Tuple Kind Tuple
1000: + <S0>
<c1>10</c1>
<c2>0.1</c2>
</S0>
1002: + <S0>
<c1>15</c1>
<c2>0.14</c2>
</S0>
2000: - <S0>
<c1>10</c1>
<c2>0.1</c2>
</S0>
2002: - <S0>
<c1>15</c1>
<c2>0.14</c2>
</S0>
200000: + <S0>
<c1>20</c1>
<c2>0.2</c2>
</S0>
201000: - <S0>
<c1>20</c1>
<c2>0.2</c2>
</S0>
400000: + <S0>
<c1>30</c1>
<c2>0.3</c2>
</S0>
401000: - <S0>
<c1>30</c1>
<c2>0.3</c2>
</S0>
100000000: + <S0>
<c1>40</c1>
<c2>4.04</c2>
</S0>
100001000: - <S0>
<c1>40</c1>
<c2>4.04</c2>
</S0>
Use an xmlforest_expr to convert each of its argument parameters to XML, and then return an XML fragment that is the concatenation of these converted arguments.
You can specify an xmlforest_expr as the argument of a function expression. It is especially useful when processing SQLX streams. For more information, see "SQL/XML (SQLX)".
You can use an xmlforest_expr in the following Oracle CQL statements:
For more information, see " XMLFOREST" in the Oracle Database SQL Language Reference.
Consider the query tkdata52_q0 in Example 11-28 and the input relation tkdata52_S0 in Example 11-29. Relation tkdata52_S0 has schema (c1 integer, c2 float). The query returns the relation in Example 11-30.
Example 11-28 xmlforest_expr Query
<query id="tkdata52_q0"><![CDATA[
select
XMLFOREST( tkdata52_S0.c1, tkdata52_S0.c2)
from
tkdata52_S0 [range 1]
]]></query>
Example 11-29 xmlforest_expr Relation Input
Timestamp Tuple
1000: 10, 0.1
1002: 15, 0.14
200000: 20, 0.2
400000: 30, 0.3
h 800000
100000000: 40, 4.04
h 200000000
Example 11-30 xmlforest_expr Relation Output
Timestamp Tuple Kind Tuple
1000: + <c1>10</c1>
<c2>0.1</c2>
1002: + <c1>15</c1>
<c2>0.14</c2>
2000: - <c1>10</c1>
<c2>0.1</c2>
2002: - <c1>15</c1>
<c2>0.14</c2>
200000: + <c1>20</c1>
<c2>0.2</c2>
201000: - <c1>20</c1>
<c2>0.2</c2>
400000: + <c1>30</c1>
<c2>0.3</c2>
401000: - <c1>30</c1>
<c2>0.3</c2>
100000000: + <c1>40</c1>
<c2>4.04</c2>
100001000: - <c1>40</c1>
<c2>4.04</c2>
Use an xml_parse_expr expression to parse and generate an XML instance from the evaluated result of arith_expr.

When using an xml_parse_expr expression, note the following:
If arith_expr resolves to null, then the expression returns null.
If you specify content, then arith_expr must resolve to a valid XML value. For an example, see "xml_parse_expr Document Example"
If you specify document, then arith_expr must resolve to a singly rooted XML document. For an example, see "xml_parse_expr Content Example".
When you specify wellformed, you are guaranteeing that value_expr resolves to a well-formed XML document, so the database does not perform validity checks to ensure that the input is well formed. For an example, see "xml_parse_expr Wellformed Example".
You can specify an xml_parse_expr as the argument of a function expression. It is especially useful when processing SQLX streams. For more information, see "SQL/XML (SQLX)".
You can use an xml_parse_expr in the following Oracle CQL statements:
For more information, see " XMLPARSE" in the Oracle Database SQL Language Reference.
This section describes the following xml_parse_expr examples:
xml_parse_expr Content Example
Consider the query tkdata62_q3 in Example 11-31 and the input relation tkdata62_S1 in Example 11-32. Relation tkdata62_S1 has schema (c1 char(30)).The query returns the relation in Example 11-33.
Example 11-31 xml_parse_expr Content: Query
<query id="tkdata62_q3"><![CDATA[
select XMLPARSE(CONTENT c1) from tkdata62_S1
]]></query>
Example 11-32 xml_parse_expr Content: Relation Input
Timestamp Tuple 1000 "<a>3</a>" 1000 "<e3>blaaaaa</e3>" 1000 "<r>4</r>" 1000 "<a></a> 1003 "<a>s3</a>" 1004 "<d>b6</d>"
Example 11-33 xml_parse_expr Content: Relation Output
Timestamp Tuple Kind Tuple 1000: + <a>3</a> 1000: + <e3>blaaaaa</e3> 1000: + <r>4</r> 1000: + <a/> 1003: + <a>s3</a> 1004: + <d>b6</d>
xml_parse_expr Document Example
Consider the query tkdata62_q4 in Example 11-34 and the input relation tkdata62_S1 in Example 11-35. Relation tkdata62_S1 has schema (c1 char(30)). The query returns the relation in Example 11-36.
Example 11-34 xml_parse_expr Document: Query
<query id="tkdata62_q4"><![CDATA[
select XMLPARSE(DOCUMENT c1) from tkdata62_S1
]]></query>
Example 11-35 xml_parse_expr Document: Relation Input
Timestamp Tuple 1000 "<a>3</a>" 1000 "<e3>blaaaaa</e3>" 1000 "<r>4</r>" 1000 "<a></a> 1003 "<a>s3</a>" 1004 "<d>b6</d>"
Example 11-36 xml_parse_expr Document: Relation Output
Timestamp Tuple Kind Tuple 1000: + <a>3</a> 1000: + <e3>blaaaaa</e3> 1000: + <r>4</r> 1000: + <a/> 1003: + <a>s3</a> 1004: + <d>b6</d>
xml_parse_expr Wellformed Example
Consider the query tkdata62_q2 in Example 11-37 and the input relation tkdata62_S in Example 11-38. Relation tkdata62_S has schema (c char(30)). The query returns the relation in Example 11-39.
Example 11-37 xml_parse_expr Wellformed: Query
<query id="tkdata62_q2"><![CDATA[
select XMLPARSE(DOCUMENT c WELLFORMED) from tkdata62_S
]]></query>