Posted by Anastasiia, March 28, 2022, 2:26 a.m.
45. Movie inventory optimization
Hi!
I've just done this task and have a doubts about dectection movies that were in demand in May 2020. At first I think about four cases:
- Rental date in may 2020:
date_trunc('month',rental_ts)='2020-05-01 00:00:00'
- Return date in may 2020:
date_trunc('month',return_ts)='2020-05-01 00:00:00' - Rental date before May 2020 and Return date after May 2020:
(rental_ts<'2020-05-01 00:00:00' and return_ts >='2020-06-01 00:00:00') - Rental date before May 2020 and return date is null (still in use)
(rental_ts<'2020-05-01 00:00:00' and return_ts is null)
But debugging queris shows that you don't count last variant. Why is it?
My full query for detecting movies that were in demand in May 2020:
SELECT i.film_id, i.inventory_id, r.rental_ts, r.return_ts
FROM film f
inner join inventory i
on i.film_id = f.film_id
inner join rental r
on i.inventory_id = r.inventory_id
and (date_trunc('month',rental_ts)='2020-05-01 00:00:00'
or date_trunc('month',return_ts)='2020-05-01 00:00:00'
or (rental_ts<'2020-05-01 00:00:00' and return_ts is null)
or (rental_ts<'2020-05-01 00:00:00'
and return_ts >='2020-06-01 00:00:00')
)