学生总和余额

时间:2018-04-03 19:01:12

标签: sql oracle

您好我有一份简单的报告,显示学生的未结余额。我想知道学生对结果进行分组的简单方法是什么,并显示欠总额?我尝试使用group by但没有工作。我添加了一张带有样本daa的图片

SELECT f.SchoolID as School,
st.lastfirst as Name,
st.grade_level as Grade,
f.Fee_Type_Name as Fee Type,
f.description as Description,
f.fee_amount as Fee Amount,
f.fee_paid as Fee Paid,
f.fee_Balance as Balance
FROM PS.FEE f
LEFT OUTER JOIN STUDENTS ST
ON f.StudentID = st.ID
WHERE (f.SCHOOLID=%param1%) AND (st.ENROLL_STATUS=0) AND (f.YEARID=%param2%) AND f.fee_balance >0
ORDER BY st.LASTFIRST

1 个答案:

答案 0 :(得分:0)

首先,您的查询使用外部联接,但where子句将逻辑转回内部联接。在这种情况下,内连接是有意义的,所以这就是你应该使用的。

接下来,如果您考虑的是每个学生,则查询应该只选择学生特征,而不是每个付款的详细信息。然后,这些细节将进入group by

这样的事情:

SELECT st.lastfirst as Name, st.grade_level as Grade,
       sum(f.fee_Balance) as Balance
FROM PS.FEE f JOIN
     STUDENTS ST
     ON f.StudentID = st.ID
WHERE f.SCHOOLID = %param1% AND st.ENROLL_STATUS = 0 AND f.YEARID = %param2% AND f.fee_balance > 0
GROUP BY st.lastfirst, st.grade_level;