-
Notifications
You must be signed in to change notification settings - Fork 30
/
zendesk__ticket_summary.sql
147 lines (136 loc) · 4.74 KB
/
zendesk__ticket_summary.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
with ticket_metrics as (
select *
from {{ ref('zendesk__ticket_metrics') }}
), user_table as (
select *
from {{ ref('stg_zendesk__user') }}
), user_sum as (
select
source_relation,
cast(1 as {{ dbt.type_int() }}) as summary_helper,
sum(case when is_active = true
then 1
else 0
end) as user_count,
sum(case when lower(role) != 'end-user' and is_active = true
then 1
else 0
end) as active_agent_count,
sum(case when is_active = false
then 1
else 0
end) as deleted_user_count,
sum(case when lower(role) = 'end-user' and is_active = true
then 1
else 0
end) as end_user_count,
sum(case when is_suspended = true
then 1
else 0
end) as suspended_user_count
from user_table
group by 1,2
), ticket_metric_sum as (
select
source_relation,
cast(1 as {{ dbt.type_int() }}) as summary_helper,
sum(case when lower(status) = 'new'
then 1
else 0
end) as new_ticket_count,
sum(case when lower(status) = 'hold'
then 1
else 0
end) as on_hold_ticket_count,
sum(case when lower(status) = 'open'
then 1
else 0
end) as open_ticket_count,
sum(case when lower(status) = 'pending'
then 1
else 0
end) as pending_ticket_count,
sum(case when lower(type) = 'problem'
then 1
else 0
end) as problem_ticket_count,
sum(case when first_assignee_id != last_assignee_id
then 1
else 0
end) as reassigned_ticket_count,
sum(case when count_reopens > 0
then 1
else 0
end) as reopened_ticket_count,
sum(case when lower(ticket_satisfaction_score) in ('offered', 'good', 'bad')
then 1
else 0
end) as surveyed_satisfaction_ticket_count,
sum(case when assignee_id is null and lower(status) not in ('solved', 'closed')
then 1
else 0
end) as unassigned_unsolved_ticket_count,
sum(case when total_agent_replies < 0
then 1
else 0
end) as unreplied_ticket_count,
sum(case when total_agent_replies < 0 and lower(status) not in ('solved', 'closed')
then 1
else 0
end) as unreplied_unsolved_ticket_count,
sum(case when lower(status) not in ('solved', 'closed')
then 1
else 0
end) as unsolved_ticket_count,
sum(case when lower(status) in ('solved', 'closed')
then 1
else 0
end) as solved_ticket_count,
sum(case when lower(status) in ('deleted')
then 1
else 0
end) as deleted_ticket_count,
sum(case when total_ticket_recoveries > 0
then 1
else 0
end) as recovered_ticket_count,
sum(case when assignee_stations_count > 0
then 1
else 0
end) as assigned_ticket_count,
count(count_internal_comments) as total_internal_comments,
count(count_public_comments) as total_public_comments,
count(total_comments)
from ticket_metrics
group by 1,2
), final as (
select
user_sum.source_relation,
user_sum.user_count,
user_sum.active_agent_count,
user_sum.deleted_user_count,
user_sum.end_user_count,
user_sum.suspended_user_count,
ticket_metric_sum.new_ticket_count,
ticket_metric_sum.on_hold_ticket_count,
ticket_metric_sum.open_ticket_count,
ticket_metric_sum.pending_ticket_count,
ticket_metric_sum.solved_ticket_count,
ticket_metric_sum.problem_ticket_count,
ticket_metric_sum.assigned_ticket_count,
ticket_metric_sum.reassigned_ticket_count,
ticket_metric_sum.reopened_ticket_count,
ticket_metric_sum.surveyed_satisfaction_ticket_count,
ticket_metric_sum.unassigned_unsolved_ticket_count,
ticket_metric_sum.unreplied_ticket_count,
ticket_metric_sum.unreplied_unsolved_ticket_count,
ticket_metric_sum.unsolved_ticket_count,
ticket_metric_sum.recovered_ticket_count,
ticket_metric_sum.deleted_ticket_count
from user_sum
left join ticket_metric_sum
on user_sum.summary_helper = ticket_metric_sum.summary_helper
and user_sum.source_relation = ticket_metric_sum.source_relation
)
select *
from final