选择播放电影最多的演员

时间:2016-12-23 09:17:50

标签: sql sql-server

我有2张桌子。 1)dbo.movi​​es

+------------+-------------+------------+
| movie_id   | movie_name  | actor_id   | 
+------------+-------------+------------+
| 1          | name1       |     1      | 
| 2          | name2       |     1      | 
| 3          | name3       |     4      | 
| 4          | name4       |     2      |
| 5          | name5       |     1      |
| 6          | name6       |     5      |
| 7          | name7       |     3      |
+------------+-------------+------------+

2)dbo.actors

+------------+-------------+
| actor_id   | actor_name  | 
+------------+-------------+
| 1          | name1       | 
| 2          | name2       |
| 3          | name3       |
| 4          | name4       |
| 5          | name5       |
+------------+-------------+

我想SELECT actor_idactor_name在大多数电影中播放。

虽然我知道如何找到一个演员所播放的电影,我找不到那位演员:

select max(y.x)
from (select count(actor_id) as x from movies group by actor_id) y

6 个答案:

答案 0 :(得分:4)

加入actors表以获取名称,按计数排序并仅获取第一条记录

select top 1 a.actor_name, count(*) as cnt
from actors a
join movies m on a.actor_id = m.actor_id
group by a.actor_id, a.actor_name
order by count(*) desc 

如果两位演员的电影数量相同,我希望两者兼得?

一种方法是

select a.actor_name, count(*) as cnt
from actors a
join movies m on a.actor_id = m.actor_id
group by a.actor_id, a.actor_name
having count(*) = (select top 1 count(*) max_cnt from movies group by actor_id order by count(*) desc)

答案 1 :(得分:2)

<强>查询

根据所播放的电影数量给出排名,然后将其与演员表连接。

;with cte as (
    select num = dense_rank() over(
        order by count(actor_id) desc
    ), actor_id,  count(actor_id) as [count]
    from movies
    group by actor_id
)
select * from actors t1
where exists(
    select 1 from cte t2
    where t1.actor_id = t2.actor_id
    and t2.num = 1
);

或最后一部分可能如下所示。

select t1.actor_id, t1.actor_name
from #actors t1
join cte t2
on t1.actor_id = t2.actor_id
where t2.num = 1;

答案 2 :(得分:2)

Check This.

using MAX :

        select movie_name, max(TotalMovies) from 
        (

        select distinct a.movie_name,count(m.actor_id) as TotalMovies
        from movies m inner join actors a on a.actor_id   = m.actor_id
        group by movie_name  

        )a
        group by movie_name

display All Actors with TotalMovies

        select distinct a.movie_name,count(m.actor_id) as TotalMovies
        from movies m inner join actors a on a.actor_id   = m.actor_id
        group by movie_name  
        order by TotalMovies desc

Using TOP 1 :

        select distinct top 1 a.movie_name,count(m.actor_id) as TotalMovies
        from movies m inner join actors a on a.actor_id   = m.actor_id
        group by movie_name
        order by TotalMovies desc

答案 3 :(得分:1)

放入另一个选择

select * from  dbo.actors where actor_id = (select max(y.x)
from (select count(actor_id) as x from movies group by actor_id) y)

答案 4 :(得分:1)

只需从子查询中选择actor_id,按降序排列并选择最高记录。

SELECT TOP 1 COUNT(actor_id) AS x,actor_id AS z FROM 
               movies GROUP BY actor_id ORDER BY COUNT(actor_id) DESC

第二个问题

SELECT *,(SELECT actor_name FROM actors WHERE actor_id=Z.actor_id) AS Name FROM 
        (SELECT COUNT(actor_id) AS X,actor_id FROM movies GROUP BY actor_id) AS Z
WHERE X=(SELECT MAX(Y) FROM 
        (SELECT COUNT(actor_id) AS Y FROM 
            movies GROUP BY actor_id)) 

答案 5 :(得分:1)

试试这个:

SELECT count(actor.actor_id),actor.actor_name FROM movies join actor on actor.actor_id=movies.actor_id group by  actor.actor_id,actor.actor_name having count(movies.actor_id) =(
SELECT MAX(counted) FROM
(
    SELECT COUNT(actor_id) AS counted
    FROM movies
    GROUP BY actor_id
) AS counts
)