-
Notifications
You must be signed in to change notification settings - Fork 43
/
create-a-session-bar-chart.sql
121 lines (107 loc) · 3.1 KB
/
create-a-session-bar-chart.sql
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
/*
Table: Sessions
+---------------------+---------+
| Column Name | Type |
+---------------------+---------+
| session_id | int |
| duration | int |
+---------------------+---------+
session_id is the primary key for this table.
duration is the time in seconds that a user has visited the application.
You want to know how long a user visits your application. You need to create bins of “[0-5>”, “[5-10>”, “[10-15>” and “15 minutes or more” and count the number of sessions on it.
Write an SQL query to report the (bin, total) in any order.
The query result format is in the following example.
Sessions table:
+-------------+---------------+
| session_id | duration |
+-------------+---------------+
| 1 | 30 |
| 2 | 299 |
| 3 | 340 |
| 4 | 580 |
| 5 | 1000 |
+-------------+---------------+
Result table:
+--------------+--------------+
| bin | total |
+--------------+--------------+
| [0-5> | 3 |
| [5-10> | 1 |
| [10-15> | 0 |
| 15 or more | 1 |
+--------------+--------------+
For session_id 1, 2 and 3 have a duration greater or equal than 0 minutes and less than 5 minutes.
For session_id 4 has a duration greater or equal than 5 minutes and less than 10 minutes.
There are no session with a duration greater or equial than 10 minutes and less than 15 minutes.
For session_id 5 has a duration greater or equal than 15 minutes.
*/
# V0
# V1
# https://code.dennyzhang.com/create-a-session-bar-chart
select '[0-5>' as bin, count(1) as total
from Sessions
where duration>=0 and duration < 300
union
select '[5-10>' as bin, count(1) as total
from Sessions
where duration>=300 and duration < 600
union
select '[10-15>' as bin, count(1) as total
from Sessions
where duration>=600 and duration < 900
union
select '15 or more' as bin, count(1) as total
from Sessions
where duration >= 900
# V2
# Time: O(n)
# Space: O(1)
SELECT '[0-5>' AS bin,
Count(1) AS total
FROM sessions
WHERE duration >= 0
AND duration < 300
UNION ALL
SELECT '[5-10>' AS bin,
Count(1) AS total
FROM sessions
WHERE duration >= 300
AND duration < 600
UNION ALL
SELECT '[10-15>' AS bin,
Count(1) AS total
FROM sessions
WHERE duration >= 600
AND duration < 900
UNION ALL
SELECT '15 or more' AS bin,
Count(1) AS total
FROM sessions
WHERE duration >= 900;
# Time: O(n)
# Space: O(n)
SELECT
t2.BIN,
COUNT(t1.BIN) AS TOTAL
FROM (
SELECT
CASE
WHEN duration/60 BETWEEN 0 AND 5 THEN "[0-5>"
WHEN duration/60 BETWEEN 5 AND 10 THEN "[5-10>"
WHEN duration/60 BETWEEN 10 AND 15 THEN "[10-15>"
WHEN duration/60 >= 15 THEN "15 or more"
ELSE NULL END AS BIN
FROM Sessions
) t1
RIGHT JOIN(
SELECT "[0-5>" AS BIN
UNION ALL
SELECT "[5-10>" AS BIN
UNION ALL
SELECT "[10-15>" AS BIN
UNION ALL
SELECT "15 or more" AS BIN
) t2
ON t1.bin = t2.bin
GROUP BY t2.bin
ORDER BY NULL;