Finding the decade with largest records, SQL Server

前端 未结 4 963
我寻月下人不归
我寻月下人不归 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条回答
  •  -上瘾入骨i
    2021-01-06 17:24

    You can use the LEFT function in SQL Server to get the decade from the year. The decade is the first 3 digits of the year. You can group by the decade and then count the number of movies. If you sort, or order, the results by the number of movies - the decade with the largest number of movies will be at the top. For example:

    select
    count(id) as number_of_movies,
    left(cast([year] as varchar(4)), 3) + '0s' as decade
    from movies
    group by left(cast([year] as varchar(4)), 3)
    order by number_of_movies desc
    

提交回复
热议问题