我在使用这些表和代码创建查询时遇到了一些问题,我知道“GROUP BY Branch.BranchName”会导致获取多条记录,但是,如何避免这种情况并在单个查询中做到这一点。我想要得到的是一个包含 BranchName 的表——支付的总价值——未支付的总价值
SELECT
(
SELECT SUM (DeptDesciption.DeptValue)
FROM dbo.SudentPayments
INNER JOIN dbo.Student ON dbo.SudentPayments.StudentId = dbo.Student.StudentId
INNER JOIN dbo.DeptDesciption ON SudentPayments.DeptDesciptionId = DeptDesciption.DeptDesciptionId
INNER JOIN dbo.Branch on dbo.Branch.BranchId = Student.BranchId
WHERE SudentPayments.IsDeptPayed = 0
GROUP BY Branch.BranchName
) AS Payed,
(
SELECT SUM (DeptDesciption.DeptValue)
FROM dbo.SudentPayments
INNER JOIN dbo.Student ON dbo.SudentPayments.StudentId = dbo.Student.StudentId
INNER JOIN dbo.DeptDesciption ON SudentPayments.DeptDesciptionId = DeptDesciption.DeptDesciptionId
INNER JOIN dbo.Branch on dbo.Branch.BranchId = Student.StudentId
WHERE SudentPayments.IsDeptPayed = 1
GROUP BY Branch.BranchName
) AS Notpayed,
Branch.BranchName
FROM dbo.SudentPayments
INNER JOIN dbo.Student ON dbo.SudentPayments.StudentId = dbo.Student.StudentId
INNER JOIN dbo.DeptDesciption ON SudentPayments.DeptDesciptionId = DeptDesciption.DeptDesciptionId
INNER JOIN dbo.Branch on dbo.Branch.BranchId = Student.StudentId