File tree Expand file tree Collapse file tree 1 file changed +21
-6
lines changed Expand file tree Collapse file tree 1 file changed +21
-6
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env python3
2
2
3
- '''
3
+ """
4
4
psycopg is the PostgreSQL adapter for the Python programming language.
5
5
Some database code and comments are adapted from the psycopg documentation(http://initd.org/psycopg/)
6
- '''
6
+ """
7
7
import psycopg2
8
8
9
9
def main ():
10
10
11
- question1 = """
11
+ question1 = '''
12
12
1. What are the most popular three articles of all time?
13
13
Which articles have been accessed the most?
14
14
Present this information as a sorted list with the most popular article at the top
15
- """
16
-
15
+ '''
16
+
17
17
q1_SQL = """
18
18
SELECT articles.title, COUNT(log.path) as num_access
19
19
FROM articles, log
20
- WHERE log.path = '/article/' || articles.slug
20
+ WHERE log.path = concat( '/article/', articles.slug)
21
21
GROUP BY articles.title
22
22
ORDER BY num_access DESC
23
23
LIMIT 3;
@@ -41,8 +41,23 @@ def main():
41
41
3. On which days did more than 1% of requests lead to errors?
42
42
The log table includes a column status that indicates the HTTP status code that the news site sent to the user's browser.
43
43
"""
44
+
45
+ SELECT Total .date , (Error .ViewCount * 1.0 / Total .ViewCount ) as ErrorPercentage
46
+ FROM
47
+ (SELECT date (time ), COUNT (* ) as ViewCount
48
+ FROM log
49
+ GROUP BY date (time )
50
+ ORDER BY date (time )) as Total
51
+ ,(SELECT date (time ), COUNT (* ) as ViewCount
52
+ FROM log WHERE status = '404 NOT FOUND'
53
+ GROUP BY date (time )
54
+ ORDER BY date (time )) as Error
55
+ WHERE Total .date = Error .date ;
44
56
45
57
q3_SQL = """
58
+ SELECT
59
+ FROM
60
+ WHERE
46
61
47
62
"""
48
63
You can’t perform that action at this time.
0 commit comments