Skip to content

Commit 3316c78

Browse files
Update retrieving-first-instance-of-change-event-using-conditional_change_event.md
1 parent 139c190 commit 3316c78

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed
Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
11
# Retrieving first instance of change event using conditional_change_event
22

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.
44

55
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:
66

77
|![Screen Shot 2024-12-04 at 9 09 14 PM](https://github.com/user-attachments/assets/bef4184b-5bbf-4a32-b95c-51666a2e4b09)|
88
|:--:|
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|
1010

1111
```sql
1212
with support_cases as (
1313
select *
1414
, 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+
|![Screen Shot 2024-12-05 at 6 42 19 PM](https://github.com/user-attachments/assets/53f8ed05-4681-4154-a451-7cda5ee3c254)|
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
1629
from support_case
1730
)
1831
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;
2033
```
2134

22-
|![Screen Shot 2024-12-05 at 8 12 20 AM](https://github.com/user-attachments/assets/fc8661f4-4bea-4a15-8bea-5cdcc65a67ea)|
35+
|![Screen Shot 2024-12-05 at 7 23 50 PM](https://github.com/user-attachments/assets/f10f24c8-738d-4fa5-8aad-cfc99740397f)|
2336
|:--:|
24-
|First `status_date` by Support Engineer per Case Status chang|
37+
|first `status_date` by Support Engineer per Case Status change|
2538

0 commit comments

Comments
 (0)