15. Fast movie watchers vs slow watchers

hard

Instruction

  • Write a query to return the number of fast movie watchers vs slow movie watchers.
  • fast movie watcher: by average return their rentals within 5 days.
  • slow movie watcher: takes an average of >5 days to return their rentals.
  • Most customers have multiple rentals over time, you need to first compute the number of days for each rental transaction, then compute the average on the rounded up days. e.g., if the rental period is 1 day and 10 hours, count it as 2 days.
  • Skip the rentals that have not been returned yet, e.g., return_ts IS NULL.
  • The orders of your results doesn't matter.
  • A customer can only rent one movie per transaction.

Table: rental


   col_name   | col_type
--------------+--------------------------
 rental_id    | integer
 rental_ts    | timestamp with time zone
 inventory_id | integer
 customer_id  | smallint
 return_ts    | timestamp with time zone
 staff_id     | smallint

Sample results


watcher_category | count
------------------+-------
 fast_watcher     |   112
 slow_watcher     |   487

Solution postgres

WITH average_rental_days AS (
	SELECT 
	    customer_id,        
	    AVG(EXTRACT(days FROM (return_ts - rental_ts) ) + 1) AS average_days
	FROM rental
	WHERE return_ts IS NOT NULL
	GROUP BY 1
)
SELECT CASE WHEN average_days <= 5 THEN 'fast_watcher'
            WHEN average_days > 5 THEN 'slow_watcher'
            ELSE NULL
            END AS watcher_category,
        COUNT(*)
FROM average_rental_days
GROUP BY watcher_category;
    

Explanation

This query calculates the average number of days a customer rents a movie and then categorizes them as either a "fast watcher" or a "slow watcher."

The first part of the query creates a temporary table called "average_rental_days," which calculates the average number of days each customer rented movies for. It only includes rentals that have been returned (return_ts is not null) and groups the results by customer_id.

The second part of the query uses a CASE statement to categorize the customers based on their average rental days. Customers who rent for 5 days or less are categorized as "fast watchers," while those who rent for more than 5 days are categorized as "slow watchers." Any customers with a null value for average_days are ignored.

Finally, the query counts the number of customers in each category and groups them by the watcher_category.

Last Submission postgres

Expected results



More IN, BETWEEN, LIKE, CASE WHEN questions

ID Title Level FTPR
178 Members who worked at both Microsoft and Google linkedin medium
34%
177 Purchases by platform report amazon medium
10%
176 Employees' annual bonus amazon easy
12%
160 Sellers with no sales by day ebay hard
11%
156 Cancellation rate by unbanned users lyft hard
15%
155 Driver with the highest cancellation rate lyft easy
24%
151 Salary report dropbox easy
18%
138 Happy restaurants doordash easy
14%
136 Extremely late orders doordash easy
11%
131 Churned accounts affirm hard
10%
102 Histogram by visit session duration google hard
17%
35 Film length report easy
27%
34 Stocked up movies easy
27%
33 Returning customers medium
8%
32 Unpopular movies hard
17%
31 Movies that have not been returned easy
26%
30 Inactive customers in May easy
22%
29 Second highest spend customer medium
14%
28 Film with the second largest cast medium
26%
27 Film with the largest cast easy
27%
26 Second shortest film easy
30%
25 Shortest film easy
46%
24 Films with more than 10 actors medium
19%
22 Average cost per rental transaction easy
42%
14 Good days and bad days hard
4%
13 Actors' first name medium
14%
12 Actors' last name ending in 'EN' or 'RY' easy
27%
11 Actors' last name easy
21%