0

我在 oracle 有一个问题

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

您的 SQL 失败,因为您在表连接上使用了 AS 关键字。

即你应该有:

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 group by 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

与原始(更正)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 , 角色 a 13 其中 d.Movie_ID = a.Movie_ID 14 GROUP BY d.Director_ID, A.Actor_ID 15 ) q2 16 其中 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 回答