-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanalytical_query_3_us13.py
80 lines (63 loc) · 2.02 KB
/
analytical_query_3_us13.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
#-----------------------------------------------------------------
# Boilerplate stuff
#-----------------------------------------------------------------
import psycopg2
import sys
import datetime
def heading(str):
print('-'*60)
print("** %s:" % (str,))
print('-'*60, '\n')
SHOW_CMD = False
def print_cmd(cmd):
if SHOW_CMD:
print(cmd.decode('utf-8'))
def print_rows(rows):
for row in rows:
print(row)
#------------------------------------------------------------
# QUERY DEFINITION GOES HERE
#------------------------------------------------------------
#Gets the number of reservations made in a specific city
def get_city_reservation_count(city):
tmpl = '''
SELECT count(r.confirmation_id)
FROM Reservation as r
JOIN Bookable as b on r.bookable_id = b.bookable_id
WHERE b.city = '%s';
'''
cmd = cur.mogrify(tmpl % city)
print_cmd(cmd)
cur.execute(cmd)
rows = cur.fetchall()
print_rows(rows)
print("Done.")
#------------------------------------------------------------
# Connect to database
#------------------------------------------------------------
userstory_no = 13
userstory_description = '''
As a customer service representative
I want to find the number of reservations for stays/experiences
in a specific city
So that I can recommend the top cities to visit
'''
query_description = '''
Joining Reservations and Bookable to get reservations booked
In each city and then finding the count of a given city
'''
print()
print('USER STORY: ', userstory_no)
print(userstory_description)
print(query_description)
# We are using the 'project' database
con = psycopg2.connect(database='project', user='isdb')
con.autocommit = True
cur = con.cursor()
# CALL TO QUERY GOES HERE:
get_city_reservation_count("Pittsburgh")
cur.close()
con.close()
#------------------------------------------------------------
# END OF USER STORY QUERY
#------------------------------------------------------------