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
SELECT demand_category, COUNT(*)
FROM (
SELECT
F.film_id,
CASE WHEN COUNT(R.rental_id) >1 THEN 'in demand' ELSE 'not in demand' END AS demand_category
FROM film F
LEFT JOIN INVENTORY I
ON F.film_id =I.film_id
LEFT JOIN (
SELECT inventory_id, rental_id
FROM rental
WHERE DATE(rental_ts) >= '2020-05-01'
AND DATE(rental_ts) <= '2020-05-31'
) R
ON R.inventory_id = I.inventory_id
GROUP BY F.film_id
)X
GROUP BY demand_category;
Explanation
This query is used to count the number of films that were either "in demand" or "not in demand" during May 2020.
The first part of the query is a subquery that joins the film table with the inventory table and the rental table (filtered for rentals between May 1 and May 31). It groups by film_id and assigns a category of "in demand" if the film was rented more than once during that time period, and "not in demand" if it wasn't rented at all or only rented once.
The outer query then takes the results of the subquery and groups them by the demand category, and counts the number of films in each category.
Overall, this query is useful for analyzing film demand during a specific time period and identifying trends in rental patterns.
Copied
Last Submission
postgres
No submission yet for this engine. Run and submit your query to save it here.
Copied
Expected results
Submit a query to compare against expected output.