最大查询总数

时间:2014-05-22 12:23:34

标签: sql oracle

我正在尝试编写一个查询,列出注册了最高总积分的学生。

这是我的问题:

    SELECT s.S_ID,
       s.S_LAST,
       s.S_FIRST,
       s."Total Credits"
FROM
  (SELECT q2.S_ID,
          q2.S_LAST,
          q2.S_FIRST,
          sum(q1.CREDITS) "Total Credits"
   FROM
     (SELECT COURSE_NO,
             CREDITS
      FROM COURSE) q1
   JOIN
     (SELECT s.S_ID,
             s.S_LAST,
             s.S_FIRST,
             cs.COURSE_NO
      FROM STUDENT s
      JOIN ENROLLMENT e ON s.S_ID = e.S_ID
      JOIN COURSE_SECTION cs ON e.C_SEC_ID = cs.C_SEC_ID) q2 ON q1.COURSE_NO = q2.COURSE_NO
   GROUP BY q2.S_ID,
            q2.S_LAST,
            q2.S_FIRST) q3
GROUP BY s.S_ID,
         s.S_LAST,
         s.S_FIRST,
         s."Total Credits"
WHERE "Total Credits" = max("Total Credits");

这不起作用我收到此错误:

ORA-00933: SQL command not properly ended
00933. 00000 -  "SQL command not properly ended"
*Cause:   
*Action:
Error at Line: 23 Column: 1

任何帮助将不胜感激。

5 个答案:

答案 0 :(得分:0)

SELECT q3.S_ID,
       q3.S_LAST,
       q3.S_FIRST,
      max( q3.Total Credits)
FROM
  (SELECT q2.S_ID,
          q2.S_LAST,
          q2.S_FIRST,
          sum(q1.CREDITS) Total Credits
   FROM
     (SELECT COURSE_NO,
             CREDITS
      FROM COURSE) q1
   JOIN
     (SELECT s.S_ID,
             s.S_LAST,
             s.S_FIRST,
             cs.COURSE_NO
      FROM STUDENT s
      JOIN ENROLLMENT e ON s.S_ID = e.S_ID
      JOIN COURSE_SECTION cs ON e.C_SEC_ID = cs.C_SEC_ID) q2 ON q1.COURSE_NO = q2.COURSE_NO
   GROUP BY q2.S_ID,
            q2.S_LAST,
            q2.S_FIRST) q3
GROUP BY q3.S_ID,
         q3.S_LAST,
         q3.S_FIRST;

答案 1 :(得分:0)

您可能只想使用group by和Sum()来解决这个问题,并使用sum()检查结果,其中限制为1或更多,具体取决于您想要的学生数量。

这将消除外部子查询。

答案 2 :(得分:0)

顽皮的双重发帖同样的问题!这是我给你的其他帖子的答案,同样的事情仍适用于此。

我非常肯定(没有Oracle机器来测试这个)你的别名是问题:

sum(q1.CREDITS) Total Credits

应该是

sum(q1.CREDITS) "Total Credits"

因此

max( q3.Total Credits)

应该是

max( q3."Total Credits")

或者你可以摆脱别名中的空间。但是,正如我所说,可能还有更多内容,我没有任何Oracle机器可以用来测试它。

答案 3 :(得分:0)

删除查询中的所有“噪音”

SELECT s.S_ID, s.S_LAST, s.S_FIRST, s."Total Credits"
FROM
  (SELECT ...
   FROM (...) q1
   JOIN (...) q2
  ) q3
GROUP BY s.S_ID, s.S_LAST, s.S_FIRST, s."Total Credits"
WHERE "Total Credits" = max("Total Credits");

很容易看到s在您的主要查询中不可见,同样在WHERE条件下您正在使用聚合面对细节,这是不可能的(至少不是这样)

答案 4 :(得分:0)

如果您一次建立一个加入查询,首先,您会注意到您不需要任何这些内联视图。您可以使用一个CTE定义和一个子查询非常简单地完成它。

WITH
student AS
( SELECT  1 s_id, 'Jones' s_last, 'Sally' s_first FROM dual UNION ALL
  SELECT  2, 'Smith', 'Pete' FROM dual UNION ALL
  SELECT  3, 'DeThroned', 'Kate' FROM dual
),
enrollment AS
( SELECT  1 s_id, 10 c_sec_id FROM dual UNION ALL
  select  1, 11 FROM dual UNION ALL
  select  2, 11 FROM dual UNION ALL
  SELECT  3, 12 FROM dual
),
course_section AS
( SELECT  10 c_sec_id, 101 course_no FROM dual UNION ALL
  SELECT  11, 102 FROM dual UNION ALL
  SELECT  12, 103 FROM dual
),
course AS
( SELECT  101 course_id, 1 credits FROM dual UNION ALL
  SELECT  102, 2 FROM dual UNION ALL
  SELECT  103, 3 FROM dual
),
StudentCredit AS
( SELECT  s.s_id, s_last, s_first, sum( c.credits ) AS TotalCredits
  FROM    student s
  JOIN    enrollment e
    ON    e.s_id = s.s_id
  JOIN    course_section cs
    ON    cs.c_sec_id = e.c_sec_id
  JOIN    course c
    ON    c.course_id = cs.course_no
  GROUP BY s.s_id, s_last, s_first
)
SELECT  *
FROM    StudentCredit
WHERE   TotalCredits = (
          SELECT  MAX( TotalCredits )
          FROM    StudentCredit
        );