Finding the decade with largest records, SQL Server

前端 未结 4 1016
我寻月下人不归
我寻月下人不归 2021-01-06 17:13

I have the following db diagram :

I want to find the decade (for example 1990 to 2000) that has the most number of movies. Actually it only deals with "

4条回答
  •  渐次进展
    2021-01-06 17:47

    An alternative to the string approach is to use integer division to get the decade:

    SELECT [Year]/10*10 as [Decade]
         , COUNT(*) as [CountMovies]
    FROM Movies
    GROUP BY [Year]/10*10
    ORDER BY [CountMovies] DESC
    

    This returns all, ordered by the decade(s) with the most movies. You could add a TOP (1) to only get the top, but then you'd need to consider tiebreaker scenarios to ensure you get deterministic results.

提交回复
热议问题