+---------------+----------+
| Column Name | Type |
+---------------+----------+
| user_id | int |
| session_start | datetime |
| session_end | datetime |
| session_id | int |
| session_type | enum |
+---------------+----------+
session_id is column of unique values for this table.
session_type is an ENUM (category) type of (Viewer, Streamer).
This table contains user id, session start, session end, session id and session type.
Write a solution to find the number of streaming sessions for users whose
first session was as a viewer.
Return the result table ordered by count of streaming sessions,user_idindescending order.
Input:
Sessions table:+---------+---------------------+---------------------+------------+--------------+| user_id | session_start | session_end | session_id | session_type |+---------+---------------------+---------------------+------------+--------------+|101|2023-11-0613:53:42|2023-11-0614:05:42|375| Viewer ||101|2023-11-2216:45:21|2023-11-2220:39:21|594| Streamer ||102|2023-11-1613:23:09|2023-11-1616:10:09|777| Streamer ||102|2023-11-1713:23:09|2023-11-1716:10:09|778| Streamer ||101|2023-11-2007:16:06|2023-11-2008:33:06|315| Streamer ||104|2023-11-2703:10:49|2023-11-2703:30:49|797| Viewer ||103|2023-11-2703:10:49|2023-11-2703:30:49|798| Streamer |+---------+---------------------+---------------------+------------+--------------+Output:
+---------+----------------+| user_id | sessions_count |+---------+----------------+|101|2|+---------+----------------+**Explanation**- user_id 101, initiated their initial session as a viewer on 2023-11-06 at 13:53:42, followed by two subsequent sessions as a Streamer, the count will be 2.- user_id 102, although there are two sessions, the initial session was as a Streamer, so this user will be excluded.- user_id 103 participated in only one session, which was as a Streamer, hence, it won't be considered.- User_id 104 commenced their first session as a viewer but didn't have any subsequent sessions, therefore, they won't be included in the final count.Output table is ordered by sessions count and user_id in descending order.
WITH first_session AS (
SELECT user_id, session_type,
ROW_NUMBER() OVER (PARTITION BY user_id ORDERBY session_start) AS rn
FROM Sessions
)
SELECT s.user_id, COUNT(*) AS sessions_count
FROM Sessions s
JOIN (
SELECT user_id
FROM first_session
WHERE rn =1AND session_type ='Viewer') v ON s.user_id = v.user_id
WHERE s.session_type ='Streamer'GROUPBY s.user_id
HAVING sessions_count >0ORDERBY sessions_count DESC, s.user_id DESC;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
WITH first_session AS (
SELECT user_id, session_type,
ROW_NUMBER() OVER (PARTITION BY user_id ORDERBY session_start) AS rn
FROM Sessions
)
SELECT s.user_id, COUNT(*) AS sessions_count
FROM Sessions s
JOIN (
SELECT user_id
FROM first_session
WHERE rn =1AND session_type ='Viewer') v ON s.user_id = v.user_id
WHERE s.session_type ='Streamer'GROUPBY s.user_id
HAVINGCOUNT(*) >0ORDERBY sessions_count DESC, s.user_id DESC;
1
2
3
4
5
6
7
8
9
10
11
import pandas as pd
defviewers_turned_streamers(sessions: pd.DataFrame) -> pd.DataFrame:
# Find first session type for each user first = sessions.sort_values(['user_id', 'session_start']).groupby('user_id').first().reset_index()
viewers = set(first[first['session_type'] =='Viewer']['user_id'])
# Count streamer sessions for these users filtered = sessions[(sessions['user_id'].isin(viewers)) & (sessions['session_type'] =='Streamer')]
result = filtered.groupby('user_id').size().reset_index(name='sessions_count')
result = result[result['sessions_count'] >0]
result = result.sort_values(['sessions_count', 'user_id'], ascending=[False, False])
return result