0

オラクルで質問です

SELECT q2.Director_ID, q2.Actor_ID
   FROM (SELECT MAX(n.Num_Joint_Movies) AS Max_Joint_Movies
      FROM (SELECT d.Director_ID, A.Actor_ID, COUNT(*) AS Num_Joint_Movies
              FROM Movies_Directors AS d
              , Roles AS a
                where d.Movie_ID = a.Movie_ID
             GROUP BY d.Director_ID, A.Actor_ID
           ) AS n
   ) AS q3,
(SELECT d.Director_ID, A.Actor_ID, COUNT(*) AS Num_Joint_Movies
      FROM Movies_Directors AS d
     , Roles AS a
       where d.Movie_ID = a.Movie_ID
     GROUP BY d.Director_ID, A.Actor_ID
    ) AS q2
where q3.Max_Joint_Movies = q2.Num_Joint_Movies

しかし、エラーORA-00907が発生しています:右括弧がありません

私が間違っていることを教えてください。

4

2 に答える 2

1

テーブル結合で AS キーワードを使用したため、SQL が失敗しています。

つまり、次のようにする必要があります。

SELECT q2.Director_ID, q2.Actor_ID
   FROM (SELECT MAX(n.Num_Joint_Movies) AS Max_Joint_Movies
      FROM (SELECT d.Director_ID, A.Actor_ID, COUNT(*) AS Num_Joint_Movies
              FROM Movies_Directors  d
              , Roles  a
                where d.Movie_ID = a.Movie_ID
             GROUP BY d.Director_ID, A.Actor_ID
           ) n
   )  q3,
(SELECT d.Director_ID, A.Actor_ID, COUNT(*) AS Num_Joint_Movies
      FROM Movies_Directors  d
     , Roles  a
       where d.Movie_ID = a.Movie_ID
     GROUP BY d.Director_ID, A.Actor_ID
    )  q2
where q3.Max_Joint_Movies = q2.Num_Joint_Movies;

ただし、SQLは次のように簡略化できます。

select Director_ID, Actor_ID, Num_Joint_Movies
  from (select d.Director_ID, A.Actor_ID, COUNT(*) Num_Joint_Movies,
               rank() over (order by count(*) desc) r
                  from Movies_Directors  d
                       inner join Roles  a
                               on d.Movie_ID = a.Movie_ID
                 group by d.Director_ID, A.Actor_ID)
 where r = 1;

小さなサンプルを編集します。

SQL> create table Movies_Directors(director_id, Movie_ID)
  2  as
  3  select 1, 1 from dual
  4  union all
  5  select 1, 2 from dual
  6  union all
  7  select 2, 3 from dual
  8  union all
  9  select 2, 4 from dual
 10  union all
 11  select 3, 1 from dual ;

Table created.

SQL> create table roles(movie_id, actor_Id)
  2  as
  3  select 1, 1 from dual union all
  4  select 1, 2 from dual union all
  5  select 1, 3 from dual union all
  6  select 1, 4 from dual union all
  7  select 2, 1 from dual union all
  8  select 2, 3 from dual union all
  9  select 3, 3 from dual union all
 10  select 3, 1 from dual;

Table created.

ランク分析を追加する場合: SQL> select d.Director_ID, A.Actor_ID, COUNT(*) Num_Joint_Movies, 2 rank() over (order by count(*) desc) r 3 from Movies_Directors d 4 inner join Roles a 5 on d.Movie_ID = a.Movie_ID 6 グループ d.Director_ID、A.Actor_ID 7 /

DIRECTOR_ID   ACTOR_ID NUM_JOINT_MOVIES          R
----------- ---------- ---------------- ----------
          1          1                2          1
          1          3                2          1
          2          3                1          3
          1          2                1          3
          3          2                1          3
          3          3                1          3
          3          4                1          3
          2          1                1          3
          1          4                1          3
          3          1                1          3

10 rows selected.

ランク1でフィルタリングするだけです..

SQL> select Director_ID, Actor_ID, Num_Joint_Movies
  2    from (select d.Director_ID, A.Actor_ID, COUNT(*) Num_Joint_Movies,
  3                 rank() over (order by count(*) desc) r
  4                               from Movies_Directors  d
  5                                    inner join Roles  a
  6                                            on d.Movie_ID = a.Movie_ID
  7                              group by d.Director_ID, A.Actor_ID)
  8   where r = 1;

DIRECTOR_ID   ACTOR_ID NUM_JOINT_MOVIES
----------- ---------- ----------------
          1          3                2
          1          1                2

vs 元の (修正済み) sql: SQL> SELECT q2.Director_ID, q2.Actor_ID 2 FROM (SELECT MAX(n.Num_Joint_Movies) A​​S Max_Joint_Movies 3 FROM (SELECT d.Director_ID, A.Actor_ID, COUNT(*) AS Num_Joint_Movies 4 FROM) Movies_Directors d 5 , Roles a 6 where d.Movie_ID = a.Movie_ID 7 GROUP BY d.Director_ID, A.Actor_ID 8 ) n 9 ) q3, 10 (SELECT d.Director_ID, A.Actor_ID, COUNT(*) AS Num_Joint_Movies 11 FROM Movies_Directors d 12 , Roles a 13 where d.Movie_ID = a.Movie_ID 14 GROUP BY d.Director_ID, A.Actor_ID 15 ) q2 16 where q3.Max_Joint_Movies = q2.Num_Joint_Movies;

DIRECTOR_ID   ACTOR_ID
----------- ----------
          1          1
          1          3

SQL>
于 2013-02-01T07:48:42.450 に答える
0
SELECT Director_ID,
    Actor_ID FROM
 (SELECT
    d.Director_ID,
    a.Actor_ID,
    COUNT(*) AS Num_Joint_Movies 
FROM
    Movies_Directors AS d 
        JOIN Roles AS a 
        ON d.Movie_ID = a.Movie_ID 
    GROUP BY
        d.Director_ID,
        a.Actor_ID
) WHERE ROWNUM = 1 ORDER BY Num_Joint_Movies ASC
/

これを試してみてください、それはうまくいくはずです

于 2013-02-01T07:11:50.243 に答える