-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathq1.py
More file actions
47 lines (34 loc) · 942 Bytes
/
q1.py
File metadata and controls
47 lines (34 loc) · 942 Bytes
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
import psycopg2
try:
conn = psycopg2.connect("dbname='a3'")
except Exception as e:
print("Unable to connect to the database")
print(e)
cur = conn.cursor()
view1 = '''create or replace view getCourseEnrolments as
select c.course_id, count(*)
from course_enrolments c
group by course_id;'''
query = '''select s.code, c.quota, g.count
from subjects s join courses c on (s.id = c.subject_id)
join terms t on (c.term_id = t.id)
join getCourseEnrolments g on (c.id = g.course_id)
where t.name like '19T3'
and c.quota > 50
and g.count > c.quota
order by s.code;'''
try:
cur.execute(view1)
except Exception as e:
print("Error creating view")
print (e)
try:
cur.execute(query)
except Exception as e:
print("Error selecting from table")
print (e)
rows = cur.fetchall()
for code,quota,count in rows:
percentOver = round(100 * count / quota)
print('{} {}%'.format(code, percentOver))
conn.close()