无法获得正确的结果sql plus

时间:2014-10-27 18:05:57

标签: sql oracle

结果显示,我无法获得正确的结果。我想整理成本列,但失败了。 总费用应该是成本的总和,但是当我分组时它变成6000+。我做的任何错误?

Select  p.title, SUM(c.cost) as Total_Fees, c.cost
    from programmes p Inner Join courses c
        ON p.programme_id = c.programme_id 
    Inner join class_schedules cs
        ON c.course_id = cs.course_id AND c.semester = 4
    Inner join class_enrollments ce
        ON cs.schedule_id = ce.schedule_id 
    Inner Join students s
        ON ce.student_id = s.student_id and s.student_id = '13WAR1001'  
    GROUP BY p.title, c.cost;

            TITLE                                                                    TOTAL_FEES       COST
--------------------------------------------------------------------------------- ---------- ----------
Bachelor of Information Technology (Honours) in Internet Technology                     1728        576
Bachelor of Information Technology (Honours) in Internet Technology                     1353        451
Bachelor of Information Technology (Honours) in Internet Technology                     1184        592
Bachelor of Information Technology (Honours) in Internet Technology                     1800        600

    TITLE                                                                                                 COURSE_ID TOTAL_FEES
---------------------------------------------------------------------------------------------------- ---------- ----------
Bachelor of Information Technology (Honours) in Internet Technology                                       14       1728
Bachelor of Information Technology (Honours) in Internet Technology                                       15       1353
Bachelor of Information Technology (Honours) in Internet Technology                                       13       1800
Bachelor of Information Technology (Honours) in Internet Technology                                       12       1184

SQL> SELECT SUM(cost) from courses where course_id = 14 or course_id = 15 or course_id = 13 or course_id = 12;

 SUM(COST)
----------
      2219

////删除c.cost后的c.cost和组

TITLE                                                                                                TOTAL_FEES
---------------------------------------------------------------------------------------------------- ----------
Bachelor of Information Technology (Honours) in Internet Technology                                     6065

3 个答案:

答案 0 :(得分:0)

您不应在选择或组中包含cost,因为您不希望根据标题和费用计算组的总和。这样做:

SELECT  
    p.title, 
    SUM(c.cost) AS Total_Fees
FROM programmes p 
INNER JOIN courses c 
    ON p.programme_id = c.programme_id 
INNER JOIN class_schedules cs 
    ON c.course_id = cs.course_id AND c.semester = 4
INNER JOIN class_enrollments ce 
    ON cs.schedule_id = ce.schedule_id 
INNER JOIN students s 
    ON ce.student_id = s.student_id AND s.student_id = '13WAR1001'  
GROUP BY p.title;

答案 1 :(得分:0)

你在GROUP BY中有c.cost而你正在c.cost列上进行SUM聚合

您需要从分组中删除cost列并仅使用标题,并从select中删除费用列。

GROUP BY p.title

答案 2 :(得分:0)

您正在按c.cost进行分组,这意味着您要按每行的成本进行分组(并且SUM实际上只显示成本)。如果您想为每个标题求和,您应该具有以下内容:

Select  p.title, SUM(c.cost) as Total_Fees
    from programmes p Inner Join courses c
        ON p.programme_id = c.programme_id 
    Inner join class_schedules cs
        ON c.course_id = cs.course_id AND c.semester = 4
    Inner join class_enrollments ce
        ON cs.schedule_id = ce.schedule_id 
    Inner Join students s
        ON ce.student_id = s.student_id and s.student_id = '13WAR1001'  
    GROUP BY p.title;
相关问题