Given two tables Activities and Age, calculate the percentage of total time spent on sending and opening snaps for each age group.
Activities Table:
activity_id(int): Unique identifier for each activityuser_id(int): User who performed the activityactivity_type(enum): Either 'send' or 'open'time_spent(decimal): Time spent on the activity
Age Table:
user_id(int): Unique user identifierage_bucket(enum): Age group ('21-25', '26-30', '31-35')
Return the percentage rounded to 2 decimal places for each age group and activity type.
Table Schema
| Column Name | Type | Description |
|---|---|---|
activity_id
PK
|
int | Unique identifier for each activity |
user_id
|
int | User who performed the activity |
activity_type
|
enum | Type of activity: 'send' or 'open' |
time_spent
|
decimal | Time spent on the activity in minutes |
| Column Name | Type | Description |
|---|---|---|
user_id
PK
|
int | Unique user identifier |
age_bucket
|
enum | Age group: '21-25', '26-30', '31-35' |
Input & Output
| activity_id | user_id | activity_type | time_spent |
|---|---|---|---|
| 1 | 1 | send | 10.5 |
| 2 | 2 | open | 5 |
| 3 | 1 | open | 2.5 |
| 4 | 3 | send | 8 |
| user_id | age_bucket |
|---|---|
| 1 | 21-25 |
| 2 | 26-30 |
| 3 | 31-35 |
| age_bucket | activity_type | percentage |
|---|---|---|
| 21-25 | open | 9.62 |
| 21-25 | send | 40.38 |
| 26-30 | open | 19.23 |
| 31-35 | send | 30.77 |
Total time spent across all activities is 26.0 minutes. The 21-25 age group spent 13.0 minutes total (10.5 sending + 2.5 opening), which represents 50% of total time. The percentages are calculated as (group_time / total_time) * 100 and rounded to 2 decimal places.
| activity_id | user_id | activity_type | time_spent |
|---|---|---|---|
| 1 | 1 | send | 15 |
| 2 | 2 | send | 10 |
| user_id | age_bucket |
|---|---|
| 1 | 21-25 |
| 2 | 26-30 |
| age_bucket | activity_type | percentage |
|---|---|---|
| 21-25 | send | 60 |
| 26-30 | send | 40 |
When all users perform only one type of activity (sending), the percentages still sum to 100% across all age groups. User 1 (21-25) spent 15 minutes out of 25 total (60%), and user 2 (26-30) spent 10 minutes out of 25 total (40%).
Constraints
-
1 ≤ activity_id ≤ 1000 -
1 ≤ user_id ≤ 100 -
activity_typeis either'send'or'open' -
0.1 ≤ time_spent ≤ 1000.0 -
age_bucketis one of'21-25','26-30','31-35'