-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
74 lines (61 loc) · 1.59 KB
/
main.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
#!/usr/bin/env python2.7
import datetime
import psycopg2
DBName = "news"
def execute(query):
db = psycopg2.connect(database=DBName)
c = db.cursor()
c.execute(query)
result = c.fetchall()
db.close()
return result
def display(query):
for key in query:
print query[key][0]
r = execute(query[key][1])
for _ in r:
print "\t \t " + _[0] + " -- " + str(_[1])
query = dict()
query[1] = ["""
What are the most popular three articles of all time?
"""]
query[2] = ["""
Who are the most popular article authors of all time?
"""]
query[3] = ["""
On which days did more than 1% of requests lead to errors?
"""]
query[1].append("""
SELECT
title,
views
FROM
article_views
ORDER BY
views
DESC
LIMIT
3
""")
query[2].append("""
SELECT
name,
SUM(views) as views
FROM
article_views
GROUP BY
name
ORDER BY
views
DESC
""")
query[3].append("""
SELECT
to_char(date, 'FMMonth DD, YYYY'),
percent_error
FROM
error_report
WHERE
percent_error > 1.00;
""")
display(query)