如何在具有相同唯一ID的列中添加所有特定值

时间:2016-08-31 14:31:35

标签: sql sql-server

是否可以使用相同的学生ID添加所有当前余额我有3个学生表,学生会话表,学生付款

我获取值的查询

select tbl_STUDENT.Student_ID AS [STUDENT ID],
tbl_STUDENT.Student_LastName + tbl_STUDENT.Student_FirstName
+ tbl_STUDENT.Student_LastName as [FULL NAME],tbl_STUDENT_SESSION.Session_Package as [PROGRAM] ,
tbl_STUDENT_SESSION.Session_EnrolledDate as [ENROLLED DATE],
tbl_STUDENT_SESSION.Session_Duedate as [DUE DATE],
tbl_STUDENT_SESSION.Total_Session as [REMAINING SESSION],
tbl_STUDENT_PAYMENT.Current_Balance as [CURRENT BALANCE] from
tbl_STUDENT inner join tbl_STUDENT_SESSION on
tbl_STUDENT.Student_ID=tbl_STUDENT_SESSION.Student_ID inner join
tbl_STUDENT_PAYMENT on
tbl_STUDENT.Student_ID=tbl_STUDENT_PAYMENT.Student_ID;

这是结果

STUDENT ID | FULL NAME | PROGRAM | ENROLLED DATE | DUE DATE     | REMAINING SESSION | CURRENT BALANCE
1126       | john doe  | 3months | August 31,2016|August 31,2016|30                 |500 
1126       | john doe  | 3months | August 31,2016|August 31,2016|30                 |6000
1126       | john doe  | 3months | August 31,2016|August 31,2016|30                 |4500

1 个答案:

答案 0 :(得分:0)

select S.student_id, s.student_firstname + s.student_lastname, ss.session_enrolledDate, ss.session_duedate, ss.total_session, sum(sp.current_balance) 
from tbl_STUDENT S 
inner join tbl_student_session SS on S.student_id = ss.student_id 
inner join tbl_student_payment SP on S.student_id = SP.student_id
group by S.student_id, s.student_firstname + s.student_lastname,
ss.session_enrolledDate, ss.session_duedate, ss.total_session