-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathint_zendesk__updates.sql
50 lines (42 loc) · 1.12 KB
/
int_zendesk__updates.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
with ticket_history as (
select *
from {{ ref('stg_zendesk__ticket_field_history') }}
), ticket_comment as (
select *
from {{ ref('stg_zendesk__ticket_comment') }}
), tickets as (
select *
from {{ ref('stg_zendesk__ticket') }}
), updates_union as (
select
source_relation,
ticket_id,
field_name,
value,
null as is_public,
user_id,
valid_starting_at,
valid_ending_at
from ticket_history
union all
select
source_relation,
ticket_id,
cast('comment' as {{ dbt.type_string() }}) as field_name,
body as value,
is_public,
user_id,
created_at as valid_starting_at,
lead(created_at) over (partition by source_relation, ticket_id order by created_at) as valid_ending_at
from ticket_comment
), final as (
select
updates_union.*,
tickets.created_at as ticket_created_date
from updates_union
left join tickets
on tickets.ticket_id = updates_union.ticket_id
and tickets.source_relation = updates_union.source_relation
)
select *
from final