-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday9.sqbpro
69 lines (61 loc) · 3.46 KB
/
day9.sqbpro
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
<?xml version="1.0" encoding="UTF-8"?><sqlb_project><db path="/Users/maximilianolopezsalgado/data_projects/sql-playground/databases/day9.db" readonly="0" foreign_keys="1" case_sensitive_like="0" temp_store="0" wal_autocheckpoint="1000" synchronous="2"/><attached/><window><main_tabs open="structure browser pragmas query" current="3"/></window><tab_structure><column_width id="0" width="300"/><column_width id="1" width="0"/><column_width id="2" width="100"/><column_width id="3" width="1500"/><column_width id="4" width="0"/><expanded_item id="0" parent="1"/><expanded_item id="1" parent="1"/><expanded_item id="2" parent="1"/><expanded_item id="3" parent="1"/></tab_structure><tab_browse><current_table name="4,9:maincran_logs"/><default_encoding codec=""/><browse_table_settings><table schema="main" name="cran_logs" show_row_id="0" encoding="" plot_x_axis="" unlock_view_pk="_rowid_"><sort/><column_widths><column index="1" value="129"/><column index="2" value="81"/><column index="3" value="91"/><column index="4" value="81"/><column index="5" value="69"/><column index="6" value="114"/><column index="7" value="196"/><column index="8" value="132"/><column index="9" value="67"/><column index="10" value="45"/></column_widths><filter_values/><conditional_formats/><row_id_formats/><display_formats/><hidden_columns/><plot_y_axes/><global_filter/></table></browse_table_settings></tab_browse><tab_sql><sql name="SQL 1">CREATE TABLE cran_logs (
download_date TEXT,
time TEXT,
size INTEGER,
r_version TEXT,
r_arch TEXT,
r_os TEXT,
package TEXT,
version TEXT,
country TEXT,
ip_id INTEGER
);
-- 9.1 Give the package name and how many times they're downloaded. Order by the 2nd column descending.
SELECT package AS Package_name, Count(*) AS Download_count
FROM cran_logs
GROUP BY package
ORDER BY 2 DESC
;
-- 9.2 Give the package ranking (based on how many times it was downloaded) during 9AM to 11AM
SELECT package, Count(*) AS download_Count
FROM cran_logs
WHERE strftime('%H:%M:%S', time) BETWEEN '09:00:00' AND '11:00:00'
GROUP BY package
ORDER BY Download_count DESC;
-- 9.3 How many records (downloads) are from China ("CN") or Japan("JP") or Singapore ("SG")?
SELECT country, COUNT(*) AS Download_count
FROM cran_logs
WHERE country IN ('CN', 'JP', 'SG')
GROUP BY country;
-- 9.4 Print the countries whose downloads are more than the downloads from China ("CN")
SELECT country, COUNT(*) AS Country_count
FROM cran_logs
GROUP BY country
HAVING COUNT(*) > (
SELECT COUNT(*)
FROM cran_logs
WHERE country = 'CN'
);
-- 9.5 Print the average length of the package name of all the UNIQUE packages
SELECT AVG(LENGTH(package)) AS Avg_lenght
FROM (
SELECT DISTINCT package
FROM cran_logs
) AS Unique_packages;
-- 9.6 Get the package whose download count ranks 2nd (print package name and its download count).
SELECT package, COUNT(*) AS Download_count
FROM cran_logs
GROUP BY package
ORDER BY Download_count DESC
LIMIT 1 OFFSET 1;
-- 9.7 Print the name of the package whose download count is bigger than 1000.
SELECT package
FROM cran_logs
GROUP BY package
HAVING COUNT(*) > 1000;
-- 9.8 The field "r_os" is the operating system of the users.
-- Here we would like to know what main system we have (ignore version number), the relevant counts, and the proportion (in percentage).
SELECT r_os AS OS_Name, COUNT(*) AS OS_Count
FROM cran_logs
ORDER BY OS_Count DESC
LIMIT 1;</sql><current_tab id="0"/></tab_sql></sqlb_project>