-
Notifications
You must be signed in to change notification settings - Fork 0
/
Que_and_Ans_29.sql
38 lines (36 loc) · 1.38 KB
/
Que_and_Ans_29.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* Q29. Get the following details for top 9 directors (based on number of movies)
Director id
Name
Number of movies
Average inter movie duration in days
Average movie ratings
Total votes
Min rating
Max rating
total movie durations */
WITH next_date_published_detail
AS( SELECT d.name_id, name, d.movie_id, duration, r.avg_rating, total_votes, m.date_published,
Lead(date_published,1) OVER(partition BY d.name_id ORDER BY date_published,movie_id )
AS next_date_published
FROM director_mapping AS d
INNER JOIN names AS n
ON n.id = d.name_id
INNER JOIN movie AS m
ON m.id = d.movie_id
INNER JOIN ratings AS r
ON r.movie_id = m.id ), top_director_summary AS
( SELECT *,
Datediff(next_date_published, date_published) AS date_difference
FROM next_date_published_detail )
SELECT name_id AS director_id,
name AS director_name,
Count(movie_id) AS number_of_movies,
Round(Avg(date_difference),2) AS avg_inter_movie_days,
Round(Avg(avg_rating),2) AS avg_rating,
Sum(total_votes) AS total_votes,
Min(avg_rating) AS min_rating,
Max(avg_rating) AS max_rating,
Sum(duration) AS total_duration
FROM top_director_summary
GROUP BY director_id
ORDER BY Count(movie_id) DESC limit 9;