









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










In this business report, I will identify the top three most watched movies along with the revenue generated by each of those movies. This could be useful to see the most popular movies, how much revenue each one creates, and if you might want to increase the price based on supply and demand. This could also tell you if it might be wise to increase your inventory of those specific movies.
Write a SQL code that creates the tables to hold your report sections. --Create detailed Table DROP TABLE IF EXISTS detailed;
CREATE OR REPLACE FUNCTION top3_rentals() RETURNS TRIGGER AS $$ BEGIN DELETE FROM summary; INSERT INTO summary( SELECT COUNT(film_id) AS rental_cnt, film_id, title, CAST(SUM(amount) AS money) AS rental_rev FROM detailed GROUP BY film_id, title ORDER BY rental_cnt DESC LIMIT 3); RETURN NEW; END; $$ LANGUAGE PLPGSQL; SELECT * FROM summary;
Write a SQL code that creates a trigger on the detailed table of the report that will continually update the summary table as data is added to the detailed table. --Create Trigger CREATE TRIGGER film_rentals AFTER INSERT ON detailed FOR EACH STATEMENT EXECUTE PROCEDURE top3_rentals();
Create a stored procedure that can be used to refresh the data in both your detailed and summary tables. The procedure should clear the contents of the detailed and summary tables and perform the ETL load process from part C and include comments that identify how often the stored procedure should be executed. --Creating Procedure CREATE OR REPLACE PROCEDURE refresh() LANGUAGE PLPGSQL AS $$ BEGIN DELETE FROM detailed; INSERT INTO detailed( rental_id, inventory_id, film_id,
title, amount ) SELECT r.rental_id, i.inventory_id, f.film_id, f.title, CAST(p.amount AS money FROM payment p INNER JOIN rental r ON r.rental_id = p.rental_id INNER JOIN inventory i ON i.inventory_id = r.inventory_id INNER JOIN film f ON f.film_id = i.film_id; END; $$; --This procedure should be ran once a month
r.rental_id, i.inventory_id, f.film_id, f.title, p.amount FROM payment p INNER JOIN rental r ON r.rental_id = p.rental_id INNER JOIN inventory i ON i.inventory_id = r.inventory_id INNER JOIN film f ON f.film_id = i.film_id; --CREATING FUNCTION CREATE OR REPLACE FUNCTION top3_rentals() RETURNS TRIGGER AS $$ BEGIN DELETE FROM summary; INSERT INTO summary( SELECT COUNT(film_id) AS rental_cnt, film_id, title, SUM(amount) AS rental_rev FROM detailed GROUP BY film_id, title ORDER BY rental_cnt DESC LIMIT 3); RETURN NEW; END; $$ LANGUAGE PLPGSQL; SELECT * FROM summary; --Create Trigger CREATE TRIGGER film_rentals AFTER INSERT ON detailed FOR EACH STATEMENT EXECUTE PROCEDURE top3_rentals(); --Creating Procedure CREATE OR REPLACE PROCEDURE refresh() LANGUAGE PLPGSQL AS $$ BEGIN DELETE FROM detailed; INSERT INTO detailed( rental_id, inventory_id, film_id, title, amount ) SELECT r.rental_id, i.inventory_id,
f.film_id, f.title, p.amount FROM payment p INNER JOIN rental r ON r.rental_id = p.rental_id INNER JOIN inventory i ON i.inventory_id = r.inventory_id INNER JOIN film f ON f.film_id = i.film_id; END; $$; CALL refresh(); SELECT * FROM detailed; SELECT rental_id, inventory_id, film_id, title, amount::money FROM detailed; SELECT * FROM summary; Link to recording: https://wgu.hosted.panopto.com/Panopto/Pages/Viewer.aspx?id=3a35fa43-35d1-4e65-8c53- af940172efbd# REFERENCES: