-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.py
88 lines (75 loc) · 3.58 KB
/
queries.py
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
import os
from database import Database
hostname = os.environ['DATABASE_HOSTNAME']
port = os.environ['DATABASE_PORT']
database_name = os.environ['DATABASE_NAME']
username = os.environ['DATABASE_USER']
password = os.environ['DATABASE_PASSWORD']
db = Database(hostname, port, database_name, username, password)
with db as conn:
print('Query 1: Amount of complaints per company:')
with conn.cursor() as cursor:
result = db.query(cursor, 'SELECT \
c.name AS company, \
COUNT(*) AS amount\
FROM \
complaints AS cs \
LEFT JOIN companies AS c ON c.id = cs.company_id \
GROUP BY \
c.name')
print('company\tamount')
for r in result:
print(r[0] + '\t\t\t\t\t' + str(r[1]))
print('\r\n\r\nQuery 2: Amount of complaints with specific tag:')
with conn.cursor() as cursor:
result = db.query(cursor, 'SELECT \
t.name AS company,\
COUNT(*) AS amount\
FROM \
complaints AS cs \
LEFT JOIN complaint_tags AS ct ON ct.complaint_id = cs.id \
LEFT JOIN tags AS t ON t.id = ct.tag_id \
WHERE \
t.name = \'SERVICEMEMBER\' \
GROUP BY \
t.name')
print('company\t\t\t\t\tamount')
for r in result:
print(r[0] + '\t\t\t\t\t' + str(r[1]))
print('\r\n\r\nQuery 3: Find average response time for complaints responded in time per company:')
with conn.cursor() as cursor:
result = db.query(cursor, 'SELECT \
c.name AS company,\
AVG(cs.company_sent_date - cs.reception_date) AS elapsed \
FROM \
complaints AS cs \
LEFT JOIN companies AS c ON c.id = cs.company_id \
WHERE \
cs.timely_response = true \
GROUP BY \
company')
print('company\t\t\t\t\telapsed')
for r in result:
print(r[0] + '\t\t\t\t\t' + str(r[1]))
print('\r\n\r\nQuery 4: Find registered companies:')
with conn.cursor() as cursor:
result = db.query(cursor, 'SELECT \
COUNT(*) AS company_amount\
FROM \
companies')
print('registered companies: ' + str(result[0][0]))
print('\r\n\r\nQuery 5: Amount of complaints not responded in time per channel:')
with conn.cursor() as cursor:
result = db.query(cursor, 'SELECT \
cs.submission_channel AS channel,\
COUNT(*) AS amount \
FROM \
complaints AS cs \
WHERE \
cs.timely_response = false\
GROUP BY channel')
print('channel\t\t\t\t\tamount')
for r in result:
print(r[0] + '\t\t\t\t\t' + str(r[1]))
db.close()
exit(0)