我有一个选择声明,它将利息总额作为利息总额。这是我正在使用的sql代码。
select sum(round(a.Amount * e.average_rate / f.average_rate, 2)) as earningTotal ,
sum(d.interest_payment) as interestTotal,
sum(d.interest_paid) as NetInterest,
a.*, c.symbol, c.Abbreviation, d.*
from investment a
inner join money_offer b
on a.ORIG_ID = b.investment_orig_id and b.UPDATE_DT is null
inner join payment_plan d
on d.offer_orig_id = b.ORIG_ID and d.UPDATE_DT is null
inner join currency c
on d.currency = c.ID
inner join exchange_rates e
on e.currency_id = a.Currency
inner join exchange_rates f
on f.currency_id = a.Currency
where a.Owner = 518 and
a.UPDATE_DT is null;
现在的问题是,我现在只收取interest_payment并将其总结,但如果有值>对于第一个interest_payment现在为0.08,interest_paid(现在为零)为0,那么我应该选择第一行的interest_paid并将其添加到第二行(0.16)+和第三行0.08的interest_payment。未来可能会有更多的行。
所以现在简单来说,因为interest_paid的所有3行都是0所以它很好,但如果第一行或第二行感兴趣的值大于0,我需要取这些值并添加它interest_payment的第3行值。我正在考虑建立一个UNION,但我不知道该怎么做。