Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. /* Question Set #1 */
  2. /* Question 1 query */
  3.  
  4. WITH family_movies AS (
  5. SELECT f.film_id,
  6. f.title,
  7. c.name
  8. FROM film AS f
  9. JOIN film_category AS fc
  10. ON fc.film_id = f.film_id
  11. JOIN category AS c
  12. ON c.category_id = fc.category_id
  13. WHERE c.name IN ('Animation', 'Children', 'Classics', 'Comedy', 'Family', 'Music')
  14. )
  15.  
  16. SELECT DISTINCT(title) AS film_title,
  17. name AS category_name,
  18. COUNT(title) OVER (PARTITION BY title) AS rental_count
  19. FROM family_movies
  20. JOIN inventory AS i
  21. ON i.film_id = family_movies.film_id
  22. JOIN rental AS r
  23. ON r.inventory_id = i.inventory_id
  24. ORDER BY 2, 1
  25.  
  26. /* Question 2 query */
  27.  
  28. WITH family_movies AS (
  29. SELECT f.film_id,
  30. f.title,
  31. c.name,
  32. f.rental_duration
  33. FROM film AS f
  34. JOIN film_category AS fc
  35. ON fc.film_id = f.film_id
  36. JOIN category AS c
  37. ON c.category_id = fc.category_id
  38. WHERE c.name IN ('Animation', 'Children', 'Classics', 'Comedy', 'Family', 'Music')
  39. ),
  40. percentiles AS (
  41. SELECT film_id,
  42. rental_duration,
  43. NTILE(4) OVER (ORDER BY rental_duration) AS standard_quartile
  44. FROM film
  45. )
  46.  
  47. SELECT f.title AS film_title,
  48. f.name AS category_name,
  49. f.rental_duration,
  50. p.standard_quartile
  51. FROM family_movies AS f
  52. JOIN percentiles AS p
  53. ON f.film_id = p.film_id
  54. ORDER BY 4, 1
  55.  
  56. /* Question 3 */
  57.  
  58. WITH family_movies AS (
  59. SELECT f.film_id,
  60. f.title,
  61. c.name,
  62. f.rental_duration
  63. FROM film AS f
  64. JOIN film_category AS fc
  65. ON fc.film_id = f.film_id
  66. JOIN category AS c
  67. ON c.category_id = fc.category_id
  68. WHERE c.name IN ('Animation', 'Children', 'Classics', 'Comedy', 'Family', 'Music')
  69. ),
  70. percentiles AS (
  71. SELECT film_id,
  72. rental_duration,
  73. NTILE(4) OVER (ORDER BY rental_duration) AS standard_quartile
  74. FROM film
  75. ),
  76. family_perc AS (
  77. SELECT f.title AS film_title,
  78. f.name AS category_name,
  79. f.rental_duration,
  80. p.standard_quartile
  81. FROM family_movies AS f
  82. JOIN percentiles AS p
  83. ON f.film_id = p.film_id
  84. )
  85.  
  86. SELECT category_name,
  87. standard_quartile,
  88. COUNT(*)
  89. FROM family_perc
  90. GROUP BY 1, 2
  91. ORDER BY 1, 2
  92.  
  93. /* Question Set #2 */
  94. /* Question 1 */
  95.  
  96. SELECT DATE_PART('month', r.rental_date) AS rental_month,
  97. DATE_PART('year', r.rental_date) AS rental_year,
  98. i.store_id,
  99. COUNT(*) AS count_rentals
  100. FROM inventory AS i
  101. JOIN rental AS r
  102. ON r.inventory_id = i.inventory_id
  103. GROUP BY 1, 2, 3
  104. ORDER BY 4 DESC
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement