-
Notifications
You must be signed in to change notification settings - Fork 1
/
event_init.sql
148 lines (136 loc) · 4.55 KB
/
event_init.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
148
create or replace view tickets_by_faculties_scopes as (
select
f.faculty_id,
f.name,
count(q_r.scope) as "Reports",
count(q_q.scope) as "Q/A",
count(q_s.scope) as "Suggestion"
from faculties f
left join tickets t on (
t.faculty_id = f.faculty_id
and t.created > date(now() - interval 1 month)
)
left join queues q_r on (
t.queue_id = q_r.queue_id
and q_r.scope = 'Reports'
)
left join queues q_q on (
t.queue_id = q_q.queue_id
and q_q.scope = 'Q/A'
)
left join queues q_s on (
t.queue_id = q_s.queue_id
and q_s.scope = 'Suggestion'
)
group by f.faculty_id
);
create or replace view tickets_by_statuses as (
select
statuses.*, count(ticket_id) tickets_count
from statuses
left join (
select ticket_id, status_id
from tickets
where created > date(now() - interval 1 month)
) tickets_for_last_month
using (status_id)
group by statuses.status_id
);
create or replace view tickets_by_scopes as (
select
distinct scope, count(ticket_id) tickets_count
from queues q
left join tickets t on (
q.queue_id = t.queue_id
and t.created > date(now() - interval 1 month)
)
group by scope
);
create or replace view faculties_by_tickets as (
select
faculty_id,
name,
(select count(*) from tickets where f.faculty_id = faculty_id group by faculty_id) created_tickets_count
from faculties f
having created_tickets_count is not null
);
create event if not exists `tickets_by_faculties_scopes_stats` on schedule
every 1 hour
on completion not preserve
enable
comment 'Calculate statistics for faculties and scopes'
do
create or replace view tickets_by_faculties_scopes as (
select
f.faculty_id,
f.name,
count(q_r.scope) as "Reports",
count(q_q.scope) as "Q/A",
count(q_s.scope) as "Suggestion"
from faculties f
left join tickets t on (
t.faculty_id = f.faculty_id
and t.created > date(now() - interval 1 month)
)
left join queues q_r on (
t.queue_id = q_r.queue_id
and q_r.scope = 'Reports'
)
left join queues q_q on (
t.queue_id = q_q.queue_id
and q_q.scope = 'Q/A'
)
left join queues q_s on (
t.queue_id = q_s.queue_id
and q_s.scope = 'Suggestion'
)
group by f.faculty_id
);
create event if not exists `tickets_by_statuses_stats` on schedule
every 1 hour
on completion not preserve
enable
comment 'Calculate statistics for tickets by statuses'
do
create or replace view tickets_by_statuses as (
select
statuses.*, count(ticket_id) tickets_count
from statuses
left join (
select ticket_id, status_id
from tickets
where created > date(now() - interval 1 month)
) tickets_for_last_month
using (status_id)
group by statuses.status_id
);
create event if not exists `tickets_by_scopes_stats` on schedule
every 1 hour
on completion not preserve
enable
comment 'Calculate statistics for tickets by statuses'
do
create or replace view tickets_by_scopes as (
select
distinct scope, count(ticket_id) tickets_count
from queues q
left join tickets t on (
q.queue_id = t.queue_id
and t.created > date(now() - interval 1 month)
)
group by scope
);
create event if not exists `faculty_by_tickets_stats` on schedule
every 1 hour
on completion not preserve
enable
comment 'Calculate statistics for tickets created on faculties'
do
create or replace view faculties_by_tickets as (
select
faculty_id,
name,
(select count(*) from tickets where f.faculty_id = faculty_id group by faculty_id) created_tickets_count
from faculties f
having created_tickets_count is not null
);