Create A Session Bar Chart Problem


Description

LeetCode Problem 1435.

Table: Sessions

1
2
3
4
5
6
7
8
+---------------------+---------+
| 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 decided 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.

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
Sessions table:
+-------------+---------------+
| session_id  | duration      |
+-------------+---------------+
| 1           | 30            |
| 2           | 199           |
| 3           | 299           |
| 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.


MySQL Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
select '[0-5>' as bin, count(session_id) as total
from Sessions
where duration < 300

union

select '[5-10>' as bin, count(session_id) as total
from Sessions
where duration >= 300 and duration < 600

union

select '[10-15>' as bin, count(session_id) as total
from Sessions
where duration < 900 and duration >= 600

union

select '15 or more' as bin, count(session_id) as total
from Sessions
where duration >= 900




Related Posts

Active Users Problem

LeetCode 1454. Write an SQL query to find the id...

Rectangles Area Problem

LeetCode 1459. Write an SQL query to report of all...

Evaluate Boolean Expression Problem

LeetCode 1440. Write an SQL query to evaluate the boolean...

Calculate Salaries Problem

LeetCode 1468. Write an SQL query to find the salaries...

Apples & Oranges Problem

LeetCode 1445. Write an SQL query to report the difference...

Create A Session Bar Chart Problem

LeetCode 1435. You want to know how long a user...