I'm trying write a query that would return the longest film rental duration and the customer who rented the said film (and also the title of the film) in the Sakila database.
As a first step, I've wrote the following query, which is supposed to return the film_id, inventory_id, customer_id and the longest duration of rental (in weeks):
SELECT DDIFF.*
FROM (SELECT rental.inventory_id, rental.customer_id, DATEDIFF(rental.return_date, rental.rental_date)/7 AS TM FROM rental) AS DDIFF
INNER JOIN (SELECT DDIFF.inventory_id, DDIFF.customer_id, MAX(DDIFF.TM) AS WEEKS
FROM (SELECT rental.inventory_id, rental.customer_id, DATEDIFF(rental.return_date, rental.rental_date)/7 AS TM FROM rental) AS DDIFF
GROUP BY DDIFF.inventory_id, DDIFF.customer_id) AS MXLST
ON DDIFF.TM = MXLST.WEEKS AND MXLST.inventory_id = DDIFF.inventory_id AND MXLST.customer_id = DDIFF.customer_id
;
yet I'm getting some unexpected results (it should return multiple rows with the longest duration being 1.4286 weeks, but I'm getting 1.0 weeks for some reason). What am I doing wrong? I'd be very thankful to hear an explanation.
Note that I've tried following this and this to properly use MAX() yet it still doesn't return the correct results.
Edit: Sakila tables` structure can be found here