|
1 | 1 | # Retrieving first instance of change event using conditional_change_event
|
2 | 2 |
|
3 |
| -`CONDITIONAL_CHANGE_EVENT` is a versatile Snowflake function that returns a window event number for each row when the value of the expression is different from the value in the previous row. |
| 3 | +Conditional Change Event (`CONDITIONAL_CHANGE_EVENT`) is a versatile Snowflake function that returns a window event number for each row when the value of the expression is different from the value in the previous row. |
4 | 4 |
|
5 | 5 | This can be a very powerful function to use when analyzing events data or time-series data. For e.g. if you need to retrieve the first `update_date` by Support Engineer per Case Status change in the following data:
|
6 | 6 |
|
7 | 7 | ||
|
8 | 8 | |:--:|
|
9 |
| -|We need to retrieve the first `status_date` by Support Engineer per Case Status change| |
| 9 | +|We need the first `status_date` by Support Engineer per Case Status change| |
10 | 10 |
|
11 | 11 | ```sql
|
12 | 12 | with support_cases as (
|
13 | 13 | select *
|
14 | 14 | , conditional_change_event (status || support_engineer)
|
15 |
| - over (partition by case_number order by update_date asc) as dense_sequence |
| 15 | + over (partition by case_number order by update_date asc) as change_rank_number |
| 16 | + from support_case |
| 17 | +); |
| 18 | +``` |
| 19 | +|| |
| 20 | +|:--:| |
| 21 | +|`change_rank_number` increases each time the Support Engineer + Status combination changes| |
| 22 | + |
| 23 | + |
| 24 | +```sql |
| 25 | +with support_cases as ( |
| 26 | + select * |
| 27 | + , conditional_change_event (status || support_engineer) |
| 28 | + over (partition by case_number order by update_date asc) as change_rank_number |
16 | 29 | from support_case
|
17 | 30 | )
|
18 | 31 | select * from support_cases
|
19 |
| -qualify row_number() over (partition by case_number, dense_sequence order by update_date) = 1; |
| 32 | +qualify row_number() over (partition by case_number, change_rank_number order by update_date) = 1; |
20 | 33 | ```
|
21 | 34 |
|
22 |
| -|| |
| 35 | +|| |
23 | 36 | |:--:|
|
24 |
| -|First `status_date` by Support Engineer per Case Status chang| |
| 37 | +|first `status_date` by Support Engineer per Case Status change| |
25 | 38 |
|
0 commit comments