Advertisement
Jetrois

csn1

Dec 17th, 2023
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --  Rental Data
  2. -- C. Provide SQL code in a text format that creates the detailed and summary tables to hold your report table sections.
  3.  
  4. CREATE TABLE all_rentals AS
  5. SELECT
  6.     r.rental_id,
  7.     i.film_id,
  8.     f.title,
  9.     f.rental_rate
  10. FROM rental r
  11. INNER JOIN inventory i ON r.inventory_id = i.inventory_id
  12. INNER JOIN film f ON f.film_id = i.film_id
  13. GROUP BY r.rental_id, f.title, f.rental_rate, i.film_id
  14. ORDER by r.rental_id ASC;
  15. SELECT * FROM all_rentals;
  16.  
  17. DROP TABLE all_rentals;
  18.  
  19. -- B.  Provide original code for function(s) in text format that perform the transformation(s) you identified in part A4.
  20.  
  21. --DETAILED TABLE
  22.  
  23. CREATE TABLE detailed_table AS
  24. SELECT
  25.     film_id,
  26.     title,
  27.     rental_rate::money,
  28.     COUNT(film_id) AS times_rented,
  29.     (COUNT(film_id) * rental_rate)::money AS revenue
  30. FROM all_rentals ar
  31. GROUP BY film_id, title, rental_rate
  32. ORDER BY revenue DESC, title
  33. LIMIT 25;
  34. SELECT * FROM detailed_table;
  35.  
  36. DROP TABLE detailed_table;
  37.  
  38. --SUMMARY TABLE
  39. CREATE TABLE summary_table AS
  40. SELECT
  41.     title,
  42.     revenue
  43. FROM detailed_table
  44. ORDER BY REVENUE DESC, title
  45. LIMIT 25;
  46. SELECT * FROM summary_table;
  47.  
  48. DROP TABLE summary_table;
  49.  
  50. --D. Write code for functions that perform the transformations identified in A4.
  51. --functions that update tables (to be triggered periodically)
  52. CREATE OR REPLACE FUNCTION update_all_rentals()
  53. RETURNS TRIGGER
  54. LANGUAGE plpgsql
  55. AS $$
  56. BEGIN
  57.     DELETE FROM all_rentals;
  58.     INSERT INTO all_rentals
  59.     SELECT
  60.         r.rental_id,
  61.         i.film_id,
  62.         f.title,
  63.         f.rental_rate
  64.     FROM rental r
  65.     INNER JOIN inventory i ON r.inventory_id = i.inventory_id
  66.     INNER JOIN film f ON f.film_id = i.film_id
  67.     GROUP BY r.rental_id, f.title, f.rental_rate, i.film_id
  68.     ORDER by r.rental_id ASC;
  69. RETURN NEW;
  70. END;
  71. $$
  72.  
  73. CREATE OR REPLACE FUNCTION update_detailed()
  74. RETURNS TRIGGER
  75. LANGUAGE plpgsql
  76. AS $$
  77. BEGIN
  78.     DELETE FROM detailed_table;
  79.     INSERT INTO detailed_table
  80.     SELECT
  81.         film_id,
  82.         title,
  83.         rental_rate::money,
  84.         --transformed data fields vv
  85.         COUNT(film_id) AS times_rented,
  86.         (COUNT(film_id) * rental_rate)::money AS revenue
  87.     FROM all_rentals ar
  88.     GROUP BY film_id, title, rental_rate
  89.     ORDER BY revenue DESC, title
  90.     LIMIT 25;
  91.     RETURN NEW;
  92. END;
  93. $$
  94.  
  95. DROP FUNCTION update_detailed();
  96.  
  97. --update_summary function
  98. CREATE OR REPLACE FUNCTION update_summary()
  99. RETURNS TRIGGER
  100. LANGUAGE plpgsql
  101. AS $$
  102. BEGIN
  103.     --2. Update summary table
  104.     DELETE FROM summary_table;
  105.     INSERT INTO summary_table
  106.     SELECT
  107.         title,
  108.         revenue
  109.     FROM detailed_table
  110.     ORDER BY REVENUE DESC, title
  111.     LIMIT 25;
  112.     RETURN NEW;
  113. END;
  114. $$
  115.  
  116. --E. Write a SQL code that creates a trigger on the detailed report that will continually update the summary table as data is added to the detailed table.
  117. --trigger statements
  118. CREATE TRIGGER update_all_rentals
  119.     AFTER INSERT
  120.     ON rental
  121.     FOR EACH STATEMENT
  122.     EXECUTE PROCEDURE update_all_rentals();
  123.    
  124. DROP TRIGGER update_all_rentals ON rental;
  125.  
  126. --update_detailed table trigger
  127. CREATE TRIGGER update_detailed
  128.     AFTER INSERT ON all_rentals
  129.     FOR EACH STATEMENT
  130.     EXECUTE PROCEDURE update_detailed();
  131.    
  132. DROP TRIGGER update_detailed ON all_rentals;
  133.  
  134. --update_summary table trigger
  135. CREATE TRIGGER update_summary
  136.     AFTER INSERT ON detailed_table
  137.     FOR EACH STATEMENT
  138.     EXECUTE PROCEDURE update_summary();
  139.    
  140. DROP TRIGGER update_summary ON detailed_table;
  141.  
  142. /*F.Create a stored procedure that can be used to refresh the data in both your detailed and summary tables. The procedure should clear
  143. the contents of the detailed and summary tables
  144. and explain how the stored procedure can be run on a schedule to ensure data freshness*/
  145. --stored procedure that updates tables
  146. CREATE OR REPLACE PROCEDURE update_all_rentals()
  147. LANGUAGE plpgsql
  148. as $$
  149. BEGIN
  150.     DELETE FROM all_rentals;
  151.     INSERT INTO all_rentals
  152.     SELECT
  153.     r.rental_id,
  154.     i.film_id,
  155.     f.title,
  156.     f.rental_rate
  157.     FROM rental r
  158.     INNER JOIN inventory i ON r.inventory_id = i.inventory_id
  159.     INNER JOIN film f ON f.film_id = i.film_id
  160.     GROUP BY r.rental_id, f.title, f.rental_rate, i.film_id
  161.     ORDER by r.rental_id ASC;
  162.     SELECT * FROM all_rentals;
  163.     SELECT * FROM all_rentals ORDER BY rental_id DESC;
  164. END;$$;
  165.  
  166. CREATE OR REPLACE PROCEDURE update_detailed()
  167. LANGUAGE plpgsql
  168. as $$
  169. BEGIN
  170.         DELETE FROM detailed_table;
  171.         INSERT INTO detailed_table
  172.         SELECT
  173.             film_id,
  174.             title,
  175.             rental_rate::money,
  176.             COUNT(film_id) AS times_rented,
  177.             (COUNT(film_id) * rental_rate)::money AS revenue
  178.         FROM all_rentals ar
  179.         GROUP BY film_id, title, rental_rate
  180.         ORDER BY revenue DESC, title
  181.         LIMIT 25;
  182. END;$$;
  183.  
  184. CALL update_detailed();
  185.  
  186. CREATE OR REPLACE PROCEDURE update_summary()
  187. LANGUAGE plpgsql
  188. as $$
  189. BEGIN
  190.         DELETE FROM summary_table;
  191.         INSERT INTO summary_table
  192.         SELECT
  193.             title,
  194.             revenue
  195. FROM detailed_table
  196. ORDER BY REVENUE DESC, title
  197. LIMIT 25;
  198. END;
  199. $$
  200.  
  201. CALL update_summary();
  202.  
  203. --test queries
  204. SELECT COUNT(*) FROM rental; --16044
  205. SELECT COUNT(*) FROM all_rentals;
  206. SELECT * FROM rental ORDER BY rental_id DESC;
  207. SELECT * FROM all_rentals ORDER BY rental_id DESC;
  208.  
  209. INSERT INTO rental VALUES (99997, '2023-12-16 12:33:15', 900, 100, '2023-11-16 14:28:16', 1, '2022-11-16 14:28:16')
  210. DELETE FROM rental WHERE rental_id >16049;
  211.  
  212. INSERT INTO all_rentals VALUES (99998, 999, 'testeroni', 99.99), (99999, 999, 'testeroni', 99.99);
  213. DELETE FROM all_rentals WHERE rental_id > 16049;
  214.  
  215. SELECT * FROM detailed_table;
  216. SELECT * FROM summary_table;
  217.  
  218. CALL update_detailed();
  219. CALL update_summary();
  220.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement