Sending vs. Opening Snaps
This is the same question as problem #25 in the SQL Chapter of Ace the Data Science Interview!
Assume you are given the tables below containing information on Snapchat users, their ages, and their time spent sending and opening snaps. Write a query to obtain a breakdown of the time spent sending vs. opening snaps (as a percentage of total time spent on these activities) for each of the different age groups.
Output the age bucket and percentage of sending and opening snaps. Round the percentages to 2 decimal places.
|activity_type||string ('send', 'open', 'chat')|
|age_bucket||string ('21-25', '26-30', '31-25')|
For the age bucket 26-30, the time spent sending snaps was 5.67 and opening 3. The percent of time sending snaps was 5.67/(5.67+3)=65.4%, and the percent of time opening snaps was 3/(5.67+3)=34.6%.
First, we join the field from the table on the corresponding field with the table. Next, we filter for 'send' and 'open' because we have to find the percentage of sending and opening snaps and then group by .
Subsequently, we obtain the total time spent on sending and opening snaps using conditional statements for each activity type ('send' and 'open') and also find the total time spent.
Here's how the output looks like:
Then, we convert the query into a CTE called and using the formula below, we calculate the percentages of the time spent sending and opening snaps.
Percentage of time spend on sending snaps = Time spent on sending snaps / Total time spend on sending and opening snaps
When dividing 2 integers, the division / operator in PostgreSQL only considers the integer part of the results and do not deal with the remainder (ie. .123, .352). Hence, to avoid integer division which happens when we multiply by 100 and to convert the numerical values into decimal values, we multiply the values with 100.0.