Max of sum query

前端 未结 5 1879
天涯浪人
天涯浪人 2021-01-27 09:48

I\'m trying to write a query that will list student(s) enrolled with the maximum total credit points.

Here is my query:

    SELECT s.S_ID,
       s.S_LAS         


        
相关标签:
5条回答
  • 2021-01-27 09:58

    Naughty double posting of the same question! This is the answer I gave to your other posting, same thing still applies here.

    I'm pretty sure (no Oracle machine to test this) that your alias is the problem:

    sum(q1.CREDITS) Total Credits
    

    Should be

    sum(q1.CREDITS) "Total Credits"
    

    And therefore

    max( q3.Total Credits)
    

    Should be

    max( q3."Total Credits")
    

    Or you could get rid of the space in the alias. But there may be more to it than that, as I say, I've no Oracle machine handy to test that out.

    0 讨论(0)
  • 2021-01-27 10:00

    You may want to tackle this by just using the group by and Sum(), and retrieve the result with an order by sum() with limit 1 or more depending on how many students your want.

    This would eliminate the outer subquery.

    0 讨论(0)
  • 2021-01-27 10:04

    Removing all the "noise" your query become

    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");
    

    It's easy to see that s is not visible from your main query, also in the WHERE condition you're confronting a detail with an aggregation, that's not possible (at least not that way)

    0 讨论(0)
  • 2021-01-27 10:08

    If you build your query one join at a time, you will notice, first, that you don't need any of those inline views. You can do it very simply with one CTE definition and a subquery.

    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
            );
    
    0 讨论(0)
  • 2021-01-27 10:20
    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;
    
    0 讨论(0)
提交回复
热议问题