我是PL-SQL的新手,所以可以使用一些帮助。
表#1包含:
LoanIntersestRates
------------------
4.5
4.0
3.5
3.0
2.5
表#2包含:
ActualInterestRate LoanAmt
-----------------------------
4.6 356258.00
4.7 387958.25
2.6 485658.25
3.65 500562.00
4.1 434135.25
2.65 756254.02
4.5 286325.02
我需要做的是获得loanAmt的总和,其中ActualInterestRate完全是表1中的内容。 此外,需要总结loadAmts,其中actualInterestrate在每个LoanInterestRates之上是1-50个点,在每个LoanInterestRates之上50-100个点,在每个LoanInterestRates之上100个点。
对此的任何帮助将不胜感激。提前谢谢!
答案 0 :(得分:1)
如果您想获得表1中提到的费率总和,请使用以下查询
select lr.interest,sum(amount) from loaninterestrates lr, loaninterestamounts la
where lr.interest = la.interest
group by lr.interest
对于步骤2来查找范围总和,请使用此
select la1.intrange,sum(la1.amount)from
(
select la.interest,la.amount,case when Remainder(la.interest*10,10) < 0 or Remainder(la.interest*10,10) = 5
then to_char(FLOOR(la.interest) + 0.5) || '-' || to_char(FLOOR(la.interest) + 1.0)
else to_char(FLOOR(la.interest)) || '-' || to_char(FLOOR(la.interest) + 0.5 )
end as intrange
from loaninterestamounts la
) la1
group by la1.intrange
sqlfiddle:http://sqlfiddle.com/#!4/92f15/8
答案 1 :(得分:0)
嗨我有一些误解你的问题,但请检查我的sql
with t1 as
(select 4.5 as lir
from dual
union all
select 4.0
from dual
union all
select 3.5
from dual
union all
select 3.0
from dual
union all
select 2.5 from dual),
t2 as
(select 4.6 as air, 356258.00 as la
from dual
union all
select 4.7, 387958.25
from dual
union all
select 2.6, 485658.25
from dual
union all
select 3.65, 500562.00
from dual
union all
select 4.1, 434135.25
from dual
union all
select 2.65, 756254.02
from dual
union all
select 4.5, 286325.02 from dual),
t1_d as
(select lir as lir_low, lead(lir) over(order by lir) as lir_high from t1)
select t1_d.lir_low, sum(la) as sum_la
from t1_d, t2
where t2.air >= t1_d.lir_low
and ((t2.air < t1_d.lir_high) or (t1_d.lir_high is null))
group by t1_d.lir_low