oracle查询找到最大关系

时间:2013-02-01 06:57:01

标签: oracle

我在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:错过右括号

请你指导我做错了什么。

2 个答案:

答案 0 :(得分: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>选择d.Director_ID,A.Actor_ID,COUNT(*)Num_Joint_Movies,       2 rank()over(按count(*)desc排序)r       3来自Movies_Directors d       4内连接角色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)AS Max_Joint_Movies       3 FROM(SELECT d.Director_ID,A.Actor_ID,COUNT(*)AS Num_Joint_Movies       4 FROM Movies_Directors d       5,角色a       6其中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>

答案 1 :(得分: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
/

尝试这个,它应该工作