Posted by Luis, Dec. 26, 2022, 7:32 p.m.
#15 Fast movie watchers vs slow movie watchers
Hey,
I'm struggling to understand why the given solution is correct. If I break it down, the number of rounded up days for each rental is given by:
ROUND(DATEDIFF(return_ts, rental_ts) + 1)
I checked some random rentals for customer 130 using the following:
SELECT
customer_id,
rental_ts,
return_ts,
ROUND(DATEDIFF(return_ts, rental_ts) + 1)
FROM rental
WHERE return_ts IS NOT NULL AND customer_id = 130
It yielded:
| customer_id | rental_ts | return_ts | ROUND(DATEDIFF(return_ts, rental_ts) + 1) |
|---|---|---|---|
| 130 | 2020-05-24T22:53:30 | 2020-05-26T22:04:30 | 3 |
| 130 | 2020-05-29T09:25:10 | 2020-06-02T04:20:10 | 5 |
| 130 | 2020-06-16T07:55:01 | 2020-06-19T06:38:01 | 4 |
In the first case the return time is just under 2 days, but the solution seems to be arbitrarily adding 1 to make it 3 days? This may work using FLOOR(), but ROUND() rounds either up or down depending on the value. The solution then takes the AVG of these days:
AVG(ROUND(DATEDIFF(return_ts, rental_ts ) + 1))
My solution instead finds the rental period using the following:
CEILING(TIMESTAMPDIFF(HOUR, rental_ts, return_ts)/24)
| customer_id | rental_ts | return_ts | CEILING(TIMESTAMPDIFF(HOUR, rental_ts, return_ts)/24) |
|---|---|---|---|
| 130 | 2020-05-24T22:53:30 | 2020-05-26T22:04:30 | 2 |
| 130 | 2020-05-29T09:25:10 | 2020-06-02T04:20:10 | 4 |
| 130 | 2020-06-16T07:55:01 | 2020-06-19T06:38:01 | 3 |
I may be missing something since I'm pretty new to SQL (that's why I'm here), let me know if I may be making a mistake.