SQL Query for find the decade with the largest number of records

Clash Royale CLAN TAG#URR8PPPSQL Query for find the decade with the largest number of records
I have a database including the following tables:
ACTOR (id, fname, lname, gender)
MOVIE (id, name, year, rank)
DIRECTOR (id, fname, lname)
CAST (a_id, m_id, role)
MOVIE_DIRECTOR (d_id, m_id)
Now I want to retrieve the data of the following question.
Ques: "A decade is a sequence of 10 consecutive years. For example, 1965, 1966, ..., 1974 is a decade, and so is 1967, 1968, ..., 1976. Find the decade with the largest number of films"
Hint: Every movie can be seen to be the beginning of a decade. Then, the decade are all years between this movie's year and the movie's year+10.
– JimmyB
2 hours ago
You can find your answer in this link stackoverflow.com/questions/27338110/…
– Hesham Gomaa
2 hours ago
@HeshamGomaa The requirement of that answer solution is different from this one. He's looking for decades that can start on any year. Not only the year where modulus 10 of the year = 0
– LukStorms
1 hour ago
Notice that you should employ a self join.
– JimmyB
53 mins ago
3 Answers
3
Did you try something like this ?
select
distinct m.year as decade_begin,
count(rollup.id) as movies_count
from
MOVIE m,
MOVIE rollup
where
rollup.year >= decade_begin and rollup.year < decade_begin+10
group by
decade_begin
order by
movies_count;
distinct should be removed.– JimmyB
1 hour ago
distinct
The only thing still missing is to extract the record(s) with the maximum
movies_count. Most simple (MySQL) way: ORDER BY movies_count DESC LIMIT 1.– JimmyB
50 mins ago
movies_count
ORDER BY movies_count DESC LIMIT 1
I would do this by generating the years, joining in the movies, and then aggregating:
select y.year as decade_start, y.year + 9 as decade_end,
count(*) as num_movies
from (select distinct year from movies) y join
movies m
on m.year >= y.year and m.year < y.year + 10
group by y.year
order by count(*) desc
limit 1;
Only the MOVIE table seems to matter here to get that answer.
In MySql 8.x one could use a self-join on a CTE for this.
WITH MOV AS
(
SELECT year as movie_year, COUNT(id) as total_movies
FROM MOVIE
GROUP BY year
)
SELECT
m1.movie_year as decade_start,
MAX(m2.movie_year) as decade_end,
SUM(m2.total_movies) as total_movies
FROM MOV m1
LEFT JOIN MOV m2 ON (m2.movie_year BETWEEN m1.movie_year AND m1.movie_year + 9)
GROUP BY m1.movie_year
ORDER BY SUM(m2.total_movies) DESC
LIMIT 1
In MySql 5.x, which doesn't support CTE's, simply replace the MOV's for m1 and m2 by a sub-query with the query from the CTE.
@JimmyB, My golfcoding instincts agree with you. Certainly if you turn this is a solution for 5.x. But I think performance will be better if you only join the years to a decade, instead of joining all movies in a year to a all movies in a decade. Not sure how a HAVING could help here though?
– LukStorms
57 mins ago
Sorry, I was wrong about the
having:)– JimmyB
55 mins ago
having
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Homework? What have you tried?
– JimmyB
2 hours ago