-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_API.py
321 lines (273 loc) · 15.4 KB
/
database_API.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import psycopg2
from psycopg2 import Error
try:
username = input("Please insert the username of server. Leave blank for default value (postgres):\n")
passcode = input("Please insert the password of postgreSQL server:\n")
hostname = input("Please insert the host ip address. Leave blank for default value (127.0.0.1):\n")
port_number = input("Please insert the port number. Leave blank for default value (5432):\n")
db_name = input("Please insert the name of the database that you would like to connect to:\n")
if (hostname == ""):
hostname = "127.0.0.1"
if (username == ""):
username = "postgres"
if (port_number == ""):
port_number = "5432"
# Connect to an existing database
connection = psycopg2.connect(user=username,
password=passcode,
host=hostname,
port=port_number,
database=db_name)
# if the connection was successfull, then show a messsage to user
if (connection):
print("PostgreSQL successfully connected with python API!\n")
# open a cursor
cur = connection.cursor()
print("2a:")
# excecute query 2a using cursor
cur.execute('''-- Ερώτημα 2a
--Βρες όλους τους οδηγούς που σχετίζονται με συμβόλαια
--τα οποία υπεγράφησαν τον τελευταίο μήνα
(
SELECT first_name,last_name,contract_code,'Driver' AS Category
FROM contracts NATURAL JOIN drivers NATURAL JOIN drivers_info
WHERE contract_code IN
-- Νέα συμβόλαια
(
SELECT contract_code
FROM contracts
WHERE EXTRACT (YEAR FROM start_date)=(SELECT date_part('year',CURRENT_DATE)) AND EXTRACT (MONTH FROM start_date)=(SELECT date_part('month',CURRENT_DATE))
)
)
UNION
--Βρες όλους τους πελάτες που σχετίζονται με συμβόλαια
-- τα οποία υπεγράφησαν τον τελευταίο μήνα
(
SELECT first_name,last_name,contract_code,'Client' AS Category
FROM contracts NATURAL JOIN clients NATURAL JOIN clients_info
WHERE contract_code IN
-- Νέα συμβόλαια
(
SELECT contract_code
FROM contracts
WHERE EXTRACT (YEAR FROM start_date)=(SELECT date_part('year',CURRENT_DATE)) AND EXTRACT (MONTH FROM start_date)=(SELECT date_part('month',CURRENT_DATE))
)
)
ORDER BY contract_code''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("first_name =",row[0], ", last_name =",row[1], ", contract_code =",row[2], ", category =",row[3])
print("\n")
print("--------------------------------------------------------------")
print("\n")
print("2b:")
# excecute query 2b using cursor
cur.execute('''-- Ερώτημα 2b
-- Παραδοχή: Θεωρούμε ότι η τρέχουσα ημερομηνία είναι Ιούνιος του 2021 (06/2021)
SELECT contract_code,phone_number1,phone_number2
FROM contracts natural join clients natural join clients_info
WHERE contract_code IN
(
SELECT contract_code
FROM contracts
WHERE EXTRACT (YEAR FROM end_date)=2021 AND EXTRACT (MONTH FROM end_date)=7
)''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("contract_code =",row[0], ", phone_number1 =",row[1], ", phone_number2 =",row[2])
print("\n")
print("--------------------------------------------------------------")
print("\n")
print("2c:")
# excecute query 2c using cursor
cur.execute('''-- Ερώτημα 2c
SELECT COUNT(DISTINCT contract_code) AS number_of_contracts, insurance_category, EXTRACT (YEAR FROM start_date) AS contract_year
FROM contracts
WHERE EXTRACT (YEAR FROM start_date)>=2016 AND EXTRACT (YEAR FROM start_date)<=2020
GROUP BY insurance_category,EXTRACT (YEAR FROM start_date)''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("number_of_contracts =",row[0], ", insurance_category =",row[1], ", contract_year =",row[2])
print("\n")
print("--------------------------------------------------------------")
print("\n")
print("2c-variation:")
# excecute query 2c-variation using cursor
cur.execute('''-- Eρώτημα 2c (Παραλλαγή)
SELECT COUNT(DISTINCT contract_code) AS number_of_contracts, insurance_category, EXTRACT (YEAR FROM end_date) AS expired_year
FROM contracts
WHERE (EXTRACT (YEAR FROM end_date)>=2016 AND EXTRACT (YEAR FROM end_date)<=2020) AND (EXTRACT (YEAR FROM start_date)<2016 OR EXTRACT (YEAR FROM start_date)>2020)
GROUP BY insurance_category,EXTRACT (YEAR FROM end_date)''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("number_of_contracts =",row[0], ", insurance_category =",row[1], ", expired_year =",row[2])
print("\n")
print("--------------------------------------------------------------")
print("\n")
print("2d-first variation:")
# excecute query 2d (first variation) using cursor
cur.execute('''-- Ερώτημα 2d (Πρώτη παραλλαγή - σε απόλυτους αριθμούς)
WITH sum_per_category (insurance_category,sum_cost) AS
(
SELECT insurance_category,SUM(contract_cost) AS sum_cost
FROM contracts
GROUP BY insurance_category
)
SELECT insurance_category,sum_cost
FROM sum_per_category
WHERE sum_cost IN
(
SELECT MAX(sum_cost)
FROM sum_per_category
)''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("insurance_category =",row[0], ", sum_cost =",row[1])
print("\n")
print("--------------------------------------------------------------")
print("\n")
print("2d-second variation:")
# execute query 2d - second variation using cursor
cur.execute('''-- Ερώτημα 2d (Δεύτερη παραλλαγή - με αναγωγή βάσει πλήθους συμβολαίων)
WITH count_per_category (insurance_category,count_contracts) AS
(
SELECT insurance_category,COUNT(DISTINCT contract_code) AS count_contracts
FROM contracts
GROUP BY insurance_category
)
SELECT insurance_category,count_contracts
FROM count_per_category
WHERE count_contracts IN
(
SELECT MAX(count_contracts)
FROM count_per_category
)''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("insurance_category =",row[0], ", count_contracts =",row[1])
print("\n")
print("--------------------------------------------------------------")
print("\n")
print("2e:")
# execute query 2e using cursor
cur.execute('''-- Ερώτημα 2e
-- 1η παραδοχή: Θεωρούμε ότι δεν υπάρχουν αυτοκίνητα που κυκλοφόρησαν πρώτη φορά πριν τον 1980
-- 2η παραδοχή: Όσα οχήματα υπάρχουν, τόσα είναι και τα διαφορετικά συμβόλαια
WITH contracts_0_4 (first_year,count_contracts) AS
(
SELECT first_year, COUNT(DISTINCT plate_number) AS count_contracts
FROM vehicles
WHERE (SELECT date_part('year',CURRENT_DATE))-first_year <= 4
GROUP BY first_year
)
,
contracts_5_9 (first_year,count_contracts) AS
(
SELECT first_year, COUNT(DISTINCT plate_number) AS count_contracts
FROM vehicles
WHERE (SELECT date_part('year',CURRENT_DATE))-first_year <= 9 AND (SELECT date_part('year',CURRENT_DATE))-first_year >= 5
GROUP BY first_year
)
,
contracts_10_19 (first_year,count_contracts) AS
(
SELECT first_year, COUNT(DISTINCT plate_number) AS count_contracts
FROM vehicles
WHERE (SELECT date_part('year',CURRENT_DATE))-first_year <= 19 AND (SELECT date_part('year',CURRENT_DATE))-first_year >= 10
GROUP BY first_year
)
,
contracts_20plus (first_year,count_contracts) AS
(
SELECT first_year, COUNT(DISTINCT plate_number) AS count_contracts
FROM vehicles
WHERE (SELECT date_part('year',CURRENT_DATE))-first_year >=20
GROUP BY first_year
)
(SELECT '0-4' AS group,CAST(SUM(count_contracts)/5 AS DECIMAL(10,2)) AS average_contracts
FROM contracts_0_4)
UNION
(SELECT '5-9' AS group,CAST(SUM(count_contracts)/5 AS DECIMAL(10,2)) AS average_contracts
FROM contracts_5_9)
UNION
(SELECT '10-19' AS group,CAST(SUM(count_contracts)/10 AS DECIMAL(10,2)) AS average_contracts
FROM contracts_10_19)
UNION
(SELECT '20+' AS group,CAST(SUM(count_contracts)/((SELECT date_part('year',CURRENT_DATE))-1999) AS DECIMAL(10,2)) AS average_contracts
FROM contracts_20plus)''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("group =",row[0], ", average_contracts =",row[1])
print("\n")
print("--------------------------------------------------------------")
print("\n")
print("2f:")
# execute query 2f - second variation using cursor
cur.execute('''-- Ερώτημα 2f
-- Παραδοχή: Δεν υπάρχει οδηγός που να είναι άνω των 80 ετών
WITH infringements_18_24 (birth_date,infringements_number) AS
(
SELECT EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date) AS age,COUNT(infringement_code) AS infringements_number
FROM infringements NATURAL JOIN infringements_drivers NATURAL JOIN drivers_info
WHERE (EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)) >= 18 AND (EXTRACT(YEAR FROM infringement_date)-EXTRACT (YEAR FROM birth_date)) <= 24
GROUP BY EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)
)
,
infringements_25_49 (birth_date,infringements_number) AS
(
SELECT EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date) AS age,COUNT(infringement_code) AS infringements_number
FROM infringements NATURAL JOIN infringements_drivers NATURAL JOIN drivers_info
WHERE (EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)) >= 25 AND (EXTRACT(YEAR FROM infringement_date)-EXTRACT (YEAR FROM birth_date)) <= 49
GROUP BY EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)
)
,
infringements_50_69 (birth_date,infringements_number) AS
(
SELECT EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date) AS age,COUNT(infringement_code) AS infringements_number
FROM infringements NATURAL JOIN infringements_drivers NATURAL JOIN drivers_info
WHERE (EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)) >= 50 AND (EXTRACT(YEAR FROM infringement_date)-EXTRACT (YEAR FROM birth_date)) <= 69
GROUP BY EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)
)
,
infringements_70plus (birth_date,infringements_number) AS
(
SELECT EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date) AS age,COUNT(infringement_code) AS infringements_number
FROM infringements NATURAL JOIN infringements_drivers NATURAL JOIN drivers_info
WHERE (EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)) >= 70
GROUP BY EXTRACT(YEAR FROM infringement_date)-EXTRACT(YEAR FROM birth_date)
)
(SELECT '18-24' AS age_group,CAST(SUM(infringements_number)/7 AS DECIMAL(10,2)) AS average_contracts
FROM infringements_18_24)
UNION
(SELECT '25-49' AS age_group,CAST(SUM(infringements_number)/25 AS DECIMAL(10,2)) AS average_contracts
FROM infringements_25_49)
UNION
(SELECT '50-69' AS age_group,CAST(SUM(infringements_number)/20 AS DECIMAL(10,2)) AS average_contracts
FROM infringements_50_69)
UNION
(SELECT '70+' AS age_group,CAST(SUM(infringements_number)/11 AS DECIMAL(10,2)) AS average_contracts
FROM infringements_70plus)''')
# fetch rows
rows = cur.fetchall()
for row in rows:
print ("age_group =",row[0], ", average_contracts =",row[1])
print("\n")
print("--------------------------------------------------------------")
print("\n")
except (Exception, Error) as error: # problem with connection occured
print("Error while connecting to PostgreSQL", error)
finally:
try:
if (connection):
cur.close()
connection.close()
print("PostgreSQL connection is closed")
finally:
input("Press ENTER to EXIT")