无法获得正确的结果sql plus(Unable to get the correct result sql plus)

编程入门 行业动态 更新时间:2024-10-28 21:29:15
无法获得正确的结果sql plus(Unable to get the correct result sql plus)

结果显示,我无法得到正确的结果。 我想整理成本列,但失败了。 总费用应该是成本的总和但是当我分组时它变成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

I can't get the correct result, as the result shown. I want to SUM up the cost column but failed. The total fees should be sum of cost but when I group by it become 6000+. Any mistake I did?

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

//// after remove the c.cost and group by c.cost

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

最满意答案

您不应在选择或组中包含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;

You shouldn't include costin the select or the group by as you do not want to compute the sum over groups based on title and cost. Do this instead:

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;

更多推荐

本文发布于:2023-07-04 04:01:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1017278.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   sql   Unable   correct   result

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!