简体   繁体   中英

How to find the actor who has acted in the maximum number of movie and 3rd busiest actor

Got 2 tables

在此处输入图片说明

Table 1 - ACTOR : ACTOR_ID, FIRST_NAME, LAST_NAME, LAST_UPDATE

Table 2 - FILM_ACTOR : ACTOR_ID, FILM_ID, LAST_UPDATE

I have tried the below on finding who has acted in max no of movies

select top 1 concat(ACTOR.FIRST_NAME, ACTOR.LAST_NAME) as Full_name
from ACTOR
left join FILM_ACTOR on ACTOR.ACTOR_ID = FILM_ACTOR.ACTOR_ID
group by FILM_ACTOR.ACTOR_ID
order by Full_name desc;

I have tried the below on finding who has acted in max no of movies.

Your original query is quite close - you just need the proper order by clause:

select top (1) concat(a.first_name, a.last_name) as full_name
from actor a
left join film_actor fa on a.actor_id = fa.actor_id
group by a.actor_id, a.first_name, a.last_name
order by count(*) desc;

If you want the nth busiest actor, then one option is to use window functions:

select full_name
from (
    select concat(a.first_name, a.last_name) as full_name, row_number() over(order by count(*) desc) rn
    from actor a
    left join film_actor fa on a.actor_id = fa.actor_id
    group by a.actor_id, a.first_name, a.last_name
) t
where rn = 3

You can do aggregation : :

select a.*
from actor a 
where a.actor_id = (select top (1) fm.actor_id 
                    from film_actor fm 
                    group by fm.actor_id 
                    order by count(*) desc
                   );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM