- Categories:
Window functions (General)
CONDITIONAL_CHANGE_EVENT¶
Returns a window event number for each row within a window partition when the value of the argument expr1 in
the current row is different from the value of expr1 in the previous row. The window event number starts
from 0 and is incremented by 1 to indicate the number of changes so far within that window.
Syntax¶
Arguments¶
expr1This is an expression that gets compared with the expression of the previous row.
expr2This is the optional expression to partition by.
expr3This is the expression to order by within each partition.
Usage notes¶
The expression
CONDITIONAL_CHANGE_EVENT (expr1) OVER (window_frame)is calculated as:CONDITIONAL_TRUE_EVENT( <expr1> != LAG(<expr1>) OVER(window_frame)) OVER(window_frame)For more information about CONDITIONAL_TRUE_EVENT, see CONDITIONAL_TRUE_EVENT.
Examples¶
This shows how to detect the number of times that the power failed and was turned back on (i.e. the number of times that the voltage dropped to 0 or was restored). (This example assumes that sampling the voltage every 15 minutes is sufficient. Because power failures can last less than 15 minutes, you’d typically want more frequent samples, or you’d want to treat the query results as an approximation.)
Create and load the table:
This shows the samples for which the voltage was zero, whether or not those zero-volt events were part of the same power failure or different power failures.
This shows the samples, along with a column indicating whether the voltage changed:
This shows the times that the power stopped and restarted:
This shows how many times the power stopped and restarted:
This example illustrates that:
The change number within a partition changes each time the specified value changes.
NULL values are not considered a new or changed value.
The change count starts over at 0 for each partition.
Create and load the table:
Query the table:
The next example shows that:
expr1can be an expression other than a column. This query uses the expressiono_col < 15, and the output of the query shows when the value in o_col changes from a value less than 15 to a value greater than or equal to 15.expr3does not need to matchexpr1. In other words, the expression in the ORDER BY sub-clause of the OVER clause does not need to match the expression in the CONDITIONAL_CHANGE_EVENT function.
The next example compares CONDITIONAL_CHANGE_EVENT and CONDITIONAL_TRUE_EVENT:
This example also compares CONDITIONAL_CHANGE_EVENT and CONDITIONAL_TRUE_EVENT:
Here is a more extensive example: