查找具有相同ID的记录并放入同一行

时间:2018-01-01 20:10:20

标签: sql sql-server

我有三个简单的表ActorFilmFilm_actor

Film_Actor表:

 Column     | Type                        | Modifiers
------------+-----------------------------+----------
actor_id    | smallint                    | not null
film_id     | smallint                    | not null

Actor表:

 Column     | Type                        | Modifiers
------------+-----------------------------+----------
actor_id    | integer                     | not null 
first_name  | character varying(45)       | not null
last_name   | character varying(45)       | not null

Film_actor表:

 Column     | Type                        | Modifiers
------------+-----------------------------+----------
film_id     | integer                     | not null
title       | character varying(255)      | not null

我的目标是找到两个演员最多并且列出标题的演员 那些电影。按字母顺序排序结果集。此外,actor_id的{​​{1}}应低于second_actor first_actor

所以期望的输出应该如下所示:

actor_id

我能想到的最好的方法就是创建新的派生表并尝试获取数据,但是这个查询提供了很多重复的行:

first_actor     | second_actor  | title
----------------+---------------+---------------
Daniel Craig    | Eva Green     | Casino Royale

伙计们,请帮助进行查询,因为我只掌握SQL的基本知识。提前致谢!

这不是家庭作业,这是我需要做的简单例子。真实表不是电影和演员。我的知识允许加入,但我不知道如何将同一个演员放在一行。现在我限制了这些知识,但我试着学习!

2 个答案:

答案 0 :(得分:2)

您可以尝试此查询。

;WITH TwoActors AS (
    SELECT   
        ac1.actor_id first_actor_id
        , ac1.first_name + ' ' + ac1.last_name   first_actor
        , ac2.actor_id second_actor_id
        , ac2.first_name + ' ' + ac2.last_name second_actor
        ,count(*) FilmCount
    FROM
        Actor as ac1 
        INNER JOIN Film_Actor as fa1 ON fa1.actor_id = ac1.actor_id
        INNER JOIN Film_Actor as fa2 ON fa1.film_id = fa2.film_id
        INNER JOIN Actor as ac2 ON fa2.actor_id = ac2.actor_id and fa1.actor_id < ac2.actor_id
        INNER JOIN Film as fi ON fa1.film_id = fi.film_id
    GROUP BY 
        ac1.actor_id
        , ac1.first_name
        , ac1.last_name   
        , ac2.actor_id
        , ac2.first_name
        , ac2.last_name

)
SELECT * FROM TwoActors
CROSS APPLY ( SELECT fi.title , fi.film_id 
                FROM  Film_Actor fa 
                INNER JOIN Film as fi ON fa.film_id = fi.film_id 
                WHERE fa.actor_id IN ( TwoActors.first_actor_id, TwoActors.second_actor_id )
                GROUP BY fi.title , fi.film_id
                HAVING COUNT(*) > 1 )AS F
ORDER BY FilmCount DESC

答案 1 :(得分:2)

我希望它对你有用。

逐个组合所有演员,然后获取他们[放在一起的文件。

CREATE TABLE #Film_actor(actor_id INT,film_id INT )
CREATE TABLE #Actor(actor_id INT,first_name VARCHAR(100),last_name VARCHAR(100))
CREATE TABLE #film(film_id INT,title VARCHAR(255))
INSERT INTO #Actor(actor_id,first_name,last_name)
SELECT 1,'Daniel','Craig' UNION ALL
SELECT 2,'Eva','Green' UNION ALL
SELECT 3,'John','White' 
INSERT INTO #film(film_id,title)
SELECT 1,'Casino Royale' UNION ALL
SELECT 2,'Windows' UNION ALL
SELECT 3,'Film3' UNION ALL
SELECT 4,'Film4' UNION ALL
SELECT 5,'Film5' 
INSERT INTO #Film_actor(actor_id,film_id)
SELECT 1,1 UNION ALL
SELECT 2,1 UNION ALL
SELECT 3,1 UNION ALL
SELECT 1,2 UNION ALL
SELECT 3,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 2,4 UNION ALL
SELECT 3,5

SELECT a1.first_name+' '+a1.last_name AS first_actor,
       a2.first_name+' '+a2.last_name AS second_actor,
       f.title
       ,COUNT(0)OVER(PARTITION BY a1.actor_id,a2.actor_id) TotalFileCount
FROM #Actor AS a1 INNER JOIN #Actor AS a2 ON a1.actor_id!=a2.actor_id
INNER JOIN #Film_actor AS fa1 ON fa1.actor_id=a1.actor_id
INNER JOIN #Film_actor AS fa2 ON fa2.film_id=fa1.film_id AND fa2.actor_id=a2.actor_id
LEFT JOIN #film AS f ON f.film_id=fa1.film_id
first_actor second_actor    title   TotalFileCount
Daniel Craig    Eva Green   Casino Royale   1
Daniel Craig    John White  Casino Royale   2
Daniel Craig    John White  Windows 2
Eva Green   Daniel Craig    Casino Royale   1
Eva Green   John White  Casino Royale   1
John White  Daniel Craig    Windows 2
John White  Daniel Craig    Casino Royale   2
John White  Eva Green   Casino Royale   1