I'm trying to get a ranked list of Top 5
(i.e. most common) document titles, grouped by decade, for each of the 6 most recent decades.
The document titles are non-unique. There could be dozens or even hundreds of documents with the same title in any given calendar year.
The following query is as far as I've been able to go. It gives me the Top 5 titles, but only for the 'all others' Decade.
How can I modify the query to get the Top 5 titles for each of the other decades as well?
SELECT
Top 5 documentTitle AS 'Title',
RANK() OVER (PARTITION BY calendarYear ORDER BY COUNT(documentTitle) DESC) AS Rank,
COUNT(tblDocumentFact.inventionTitleEnID) AS 'Number of Occurrences',
CASE
WHEN calendarYear BETWEEN 2010 AND 2019 THEN '2010 - 2019'
WHEN calendarYear BETWEEN 2000 AND 2009 THEN '2000 - 2009'
WHEN calendarYear BETWEEN 1990 AND 1999 THEN '1990 - 1999'
WHEN calendarYear BETWEEN 1980 AND 1989 THEN '1980 - 1989'
WHEN calendarYear BETWEEN 1970 AND 1979 THEN '1970 - 1979'
WHEN calendarYear BETWEEN 1960 AND 1969 THEN '1960 - 1969'
ELSE 'all others'
END AS Decade
FROM tbldocumentTitleDimension
INNER JOIN tblDocumentFact ON tbldocumentTitleDimension.documentTitleID = tblDocumentFact.documentTitleID
INNER JOIN tblDateDimension ON tblDocumentFact.publicationDateID = tblDateDimension.dateID
GROUP BY documentTitle,
calendarYear
ORDER BY [Number of Occurrences] DESC