我从查询中扣除余额时出现问题,这是我的代码:
查询
select [EmployeeID] = emp.EmployeeID,
[FullName] = emp.FName + ' ' + emp.LName,
[LoanTypeID] = lt.LoanTypeID,
[LoanName] = lt.[Description],
[Salary] = sal.Amount,
[TotalLoanAmount] = (
select SUM(lc1.LoanAmount)
from LoanContract lc1
inner join LoanType lt1 on lt1.LoanTypeID = lc1.LoanTypeID
where lt1.LoanTypeID = lt.LoanTypeID
and lc1.EmployeeID = emp.EmployeeID
),
[PrincipalAmount] = (
select ISNULL(SUM((CAST(((lc1.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(varchar(15), LoanDateStart, 101), CONVERT(varchar(15), LoanPaymentDue, 101)) * 12) * 2)) as decimal(18, 2)))), 0)
from LoanContract lc1
inner join LoanType lt1 on lt1.LoanTypeID = lc1.LoanTypeID
where lt1.LoanTypeID = lt.LoanTypeID
and lc1.EmployeeID = emp.EmployeeID
),
[Interest] = (
select ISNULL(SUM((CAST((((lt1.InterestRate / 100) * lc1.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(varchar(15), LoanDateStart, 101), CONVERT(varchar(15), LoanPaymentDue, 101)) * 12) * 2)) as decimal(18, 2)))), 0)
from LoanContract lc1
inner join LoanType lt1 on lt.LoanTypeID = lc1.LoanTypeID
where lt1.LoanTypeID = lt.LoanTypeID
and lc1.EmployeeID = emp.EmployeeID
),
[Total] = (
select ISNULL(SUM((CAST(((((lt1.InterestRate / 100) * lc1.LoanAmount) + lc1.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(varchar(15), LoanDateStart, 101), CONVERT(varchar(15), LoanPaymentDue, 101)) * 12) * 2)) as decimal(18, 2)))), 0)
from LoanContract lc1
inner join LoanType lt1 on lt.LoanTypeID = lc1.LoanTypeID
where lt1.LoanTypeID = lt.LoanTypeID
and lc1.EmployeeID = emp.EmployeeID
),
[Balance] = (
select ISNULL(SUM((CAST(((((lt1.InterestRate / 100) * lc1.LoanAmount) + lc1.LoanAmount) / ((dbo.fnNumberOfYears(CONVERT(varchar(15), LoanDateStart, 101), CONVERT(varchar(15), LoanPaymentDue, 101)) * 12) * 2)) as decimal(18, 2)))), 0)
from LoanContract lc1
inner join LoanType lt1 on lt.LoanTypeID = lc1.LoanTypeID
where lt1.LoanTypeID = lt.LoanTypeID
and lc1.EmployeeID = emp.EmployeeID
),
[PaymentDate] = lp.DateOfPayment
from Employee emp
inner join Salary sal on sal.EmployeeID = emp.EmployeeID
inner join LoanContract lc on lc.EmployeeID = emp.EmployeeID
inner join LoanPayments lp on lp.LoanID = lc.LoanID
left join LoanType lt on lt.LoanTypeID = lc.LoanTypeID
where lc.EmployeeID in (
select EmployeeID
from Employee
where EmployeeID like case when CAST(ISNULL(@EmployeeID, 0) as varchar(15)) <> 0 then CAST(@EmployeeID as varchar(15)) else '' end + '%'
)
and lc.LoanTypeID in (
select LoanTypeID
from LoanType
where LoanTypeID like case when CAST(ISNULL(@LoanType, 0) as varchar(15)) <> 0 then CAST(@LoanType as varchar(15)) else '' end + '%'
)
and lp.DateOfPayment between case when ISNULL(@DateStart, '') <> '' then CONVERT(varchar(10), CAST(ISNULL(@DateStart, '') as date), 101) else CONVERT(varchar(10), CAST(ISNULL(GETDATE(), '') as date), 101) end and case when ISNULL(@DateEnd, '') <> '' then CONVERT(varchar(10), CAST(ISNULL(@DateEnd, '') as date), 101) else CONVERT(varchar(10), CAST(ISNULL(GETDATE(), '') as date), 101) end
group by lt.[Description],
emp.FName,
emp.LName,
sal.Amount,
lp.DateOfPayment,
emp.EmployeeID,
lc.LoanTypeID,
lt.LoanTypeID
order by lp.DateOfPayment,
emp.FName,
emp.LName,
lc.LoanTypeID asc
我想在这里尝试实现的是,我想从特定Balance
中的Total
列中获取总LoanTypeID
列。
例如:
LoanTypeID = 1, Balance = 6250, TotalLoanAmount = 150,000
因为有3 LoanTypeID = 1
然后Balance = 18750
,然后最终被扣除到TotalLoanAmount
,因此Balance
将是131,250
。
如何实现这一目标?有人可以帮我弄这个吗?唯一的问题是在Balance
列。
答案 0 :(得分:1)
这会让你接近你所需要的。
SELECT
EP.*,
LT.Loan_ID,
LT.Loan_Amount,
(SELECT
SUM(amount)
FROM LoanPayments LP
WHERE LP.emp_id = EP.emp_id
AND LP.Loan_id = LT.Load_id)
[PaymentsPerLoanAccount],
LT.Loan_Amount - (SELECT
SUM(amount)
FROM LoanPayments LP
WHERE LP.emp_id = EP.emp_id
AND LP.Loan_id = LT.Load_id)
[LoanAccountBalance]
FROM employee EP
LEFT OUTER JOIN LoanTable LT
ON -- display all employees regardless if there is a loan
LT.emp_id = EP.emp_id