银行贷款分段计息的问题!

topciv 2008-12-10 06:43:04
现在有一个(表1):
名称 日期 贷款余额 利率
公司A 2008-10-15 100
...
公司A 2009-1-1 100
公司A 2009-1-2 100
....
公司B 2009-1-1 200
公司B 2009-1-2 200
...

还有一个利率(表2):
2009-1-1 3%
2009-1-2 3%
...
2009-4-1 2.9%
...

要把每个时点的利率匹配到表1,但是,如果利率有调整,利率是3个月才调整一次。
比如公司A是2008-10-15开始贷款,那么到2009-1-15重新定一次利率,再维持3个月。
再到2009-4-15看一次利率,利率调整到了2.9%,再维持3个月。
...
如何写SQL更新表1最为方便?
...全文
1708 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
小纯洁001 2011-09-09
  • 打赏
  • 举报
回复
顶一个!!
topciv 2008-12-11
  • 打赏
  • 举报
回复
谢谢各位关注!
我是LZ,我已经解决了这个问题:
增加一个保存利率日期的字段@ratedate,作用是找出当天所采用的利率应该是那一天的?
再根据@ratedate到利率表里找出对应的利率。

因为原始数据只是贷款的开始和结束日期,所以需要循环生成贷款的各个天的数据,
在循环时就判断当天日期是否满3个月了,定义一个@startdate(最后一次计息调整时点,也就是3个月的开始,初始值为放款当天)
如果未满3个月,那么@ratedate=@startdate
如果满了,那么当天采用新利率@ratedate=当天的日期,而且将最后一次计息调整时点也调整为当天的日期,再循环3个月。
-------------------
--建立表格(id必须连续)
declare @t table(id int,dw varchar(30),dkje decimal(18,2),qx int,fkrq smalldatetime,rq smalldatetime,ratedate smalldatetime,rate decimal(18,6))
declare @dk table(id int,dw varchar(30),dkje decimal(18,2),qx int,fkrq smalldatetime,dqrq smalldatetime)
insert into @dk
select 1,'公司A',100,'6','2009-2-1','2009-7-31'
union all
select 2,'公司B',200,'12','2008-10-15','2009-10-14'
--select * from @dk

--导入数据
declare @id int
declare @i int
declare @qx int
declare @dqrq smalldatetime
declare @fkrq smalldatetime
declare @startdate smalldatetime --开始日期
declare @ratedate smalldatetime
set @id=1
set @i=0
--select @qx,@dqrq,@fkrq
while @id<=2
begin
select @qx=qx,@dqrq=dqrq,@fkrq=fkrq,@startdate=@fkrq from @dk where id=@id
while @fkrq+@i<=@dqrq
begin
--计算@ratedate-------
if dateadd(m,3,@startdate)>=dateadd(d,@i+1,@fkrq)
begin
set @ratedate=@startdate
end
else
begin
set @ratedate=dateadd(d,@i,@fkrq)
set @startdate=dateadd(d,@i,@fkrq)
end
--结束------
insert into @t select id,dw,dkje,qx,@fkrq,dateadd(d,@i,fkrq),@ratedate,0 from @dk where id=@id
set @i=@i+1
end
set @id=@id+1
set @i=0
end
select * from @t order by id,rq
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 topciv 的回复:]
一共有200笔贷款,因为每一笔贷款开始日期不一样,所以调整利率的日期都是不一样的。
[/Quote]

所以要用一个临时表记录下每个客户要调整利率的时间啊。@t就是用来记录的
zc_0101 2008-12-10
  • 打赏
  • 举报
回复
开银行的来了?
topciv 2008-12-10
  • 打赏
  • 举报
回复
一共有200笔贷款,因为每一笔贷款开始日期不一样,所以调整利率的日期都是不一样的。
dawugui 2008-12-10
  • 打赏
  • 举报
回复
这个肯定不是这么简单算的,关注之.
  • 打赏
  • 举报
回复
你的数据看不出来,上面的结果也贴错了,重新给你贴一下。另外修改了下你的数据

--> (让你望见影子的墙)生成测试数据,时间:2008-12-10

if not object_id('tb') is null
drop table tb
Go
Create table tb([名称] nvarchar(3),[日期] Datetime,[贷款余额] int,[利率]varchar(10))
Insert tb
select N'公司A','2007-10-15',100,null union all
select N'公司A','2008-1-1',100,null union all
select N'公司A','2008-1-15',100,null union all
select N'公司B','2008-1-1',200,null union all
select N'公司B','2008-1-2',200,null
Go
Select * from tb

--> (让你望见影子的墙)生成测试数据,时间:2008-12-10

if not object_id('tb2') is null
drop table tb2
Go
Create table tb2([日期] Datetime,[利率] nvarchar(4))
Insert tb2
select '2007-10-15',N'3%' union all
select '2007-1-1',N'3%' union all
select '2008-1-15',N'3.5%' union all
select '2008-1-1',N'2.9%'
Go
Select * from tb2

--> (让你望见影子的墙)生成测试数据,时间:2008-12-10
declare @t table(名称 nvarchar(3),rq datetime)
declare @name varchar(100),@date datetime
declare cur cursor for select 名称, min(日期) from tb group by 名称
open cur
fetch next from cur into @name,@date
while @@fetch_status=0
begin
insert @t values(@name,@date)
while @date<=getdate()
begin
set @date=dateadd(month,3,@date)
insert @t
select @name,@date
end
fetch next from cur into @name,@date
end
close cur
deallocate cur

update tb
set 利率=(select top 1 利率 from (select t.名称,t.rq,tb2.利率 from @t t left join tb2 on t.rq=tb2.日期 )K
where k.rq<=tb.日期 and k.名称=tb.名称 order by k.rq desc)

select * from tb
公司A 2007-10-15 00:00:00.000 100 3%
公司A 2008-01-01 00:00:00.000 100 3%
公司A 2008-01-15 00:00:00.000 100 3.5%
公司B 2008-01-01 00:00:00.000 200 2.9%
公司B 2008-01-02 00:00:00.000 200 2.9%

  • 打赏
  • 举报
回复
这个需要先构造一个时间表,来记录每个客户所要检查利率的时间,再进行更新

--> (让你望见影子的墙)生成测试数据,时间:2008-12-10

if not object_id('tb') is null
drop table tb
Go
Create table tb([名称] nvarchar(3),[日期] Datetime,[贷款余额] int,[利率]varchar(10))
Insert tb
select N'公司A','2008-10-15',100,null union all
select N'公司A','2009-1-1',100,null union all
select N'公司A','2009-1-2',100,null union all
select N'公司B','2009-1-1',200,null union all
select N'公司B','2009-1-2',200,null
Go
Select * from tb

--> (让你望见影子的墙)生成测试数据,时间:2008-12-10

if not object_id('tb2') is null
drop table tb2
Go
Create table tb2([日期] Datetime,[利率] nvarchar(4))
Insert tb2
select '2008-10-15',N'3%' union all
select '2009-1-1',N'3%' union all
select '2009-1-15',N'3%' union all
select '2009-4-1',N'2.9%'
Go
Select * from tb2

declare @t table(名称 nvarchar(3),rq datetime)
declare @name varchar(100),@date datetime
declare cur cursor for select 名称, min(日期) from tb group by 名称
open cur
fetch next from cur into @name,@date
while @@fetch_status=0
begin
insert @t values(@name,@date)
while @date<=getdate()
begin
set @date=dateadd(month,3,@date)
insert @t
select @name,@date
end
fetch next from cur into @name,@date
end
close cur
deallocate cur

update tb
set 利率=(select top 1 利率 from (select t.名称,t.rq,tb2.利率 from @t t left join tb2 on t.rq=tb2.日期 )K
where k.rq<=tb.日期 and k.名称=tb.名称 order by k.rq desc)

select * from tb
公司A 2008-10-15 00:00:00.000 2008-10-15 00:00:00.000 3%
公司A 2009-01-15 00:00:00.000 2009-01-15 00:00:00.000 3%
公司B 2009-01-01 00:00:00.000 2009-01-01 00:00:00.000 3%

azhhuoiu 2008-12-10
  • 打赏
  • 举报
回复
create table tb_dk (cmp varchar(20),dkrq datetime,je decimal(18,4),rate decimal(18,4))
insert into tb_dk
select 'a','2009-1-1',100,null union all
select 'b','2009-4-2',100,null union all
select 'c','2009-7-1',100,null union all
select 'd','2009-10-1',100,null
create table tb_rate(dt datetime,rate decimal(18,4) )
insert into tb_rate
select '2009-1-1',0.03 union all
select '2009-4-1',0.38 union all
select '2009-7-2',0.39 union all
select '2009-7-3',0.39 union all
select '2009-10-1',0.33
go
create function uf_getrateday(@starttime datetime ,@m int)
returns datetime
as
begin
declare @rq datetime
select @rq=convert(varchar(10),dateadd(mm,@m,(@starttime)),120) from tb_dk
return @rq
end
go
update tb_dk set rate=tb_rate.rate from tb_dk,tb_rate where dbo.uf_getrateday(dkrq,3)=tb_rate.dt
select * from tb_dk
-------------
a 2009-01-01 00:00:00.000 100.0000 0.3800
b 2009-04-02 00:00:00.000 100.0000 0.3900
c 2009-07-01 00:00:00.000 100.0000 0.3300
d 2009-10-01 00:00:00.000 100.0000 NULL
topciv 2008-12-10
  • 打赏
  • 举报
回复
就是把一笔贷款每天的余额都计入数据库,然后每天需要对应一个利率。
2009年的利率只是一个预测数,所以需要先填进去。
每笔贷款放款后利率维持3个月,然后每3个月后重新定一次利率。
百年树人 2008-12-10
  • 打赏
  • 举报
回复
没搞明白,帮顶
jiangshun 2008-12-10
  • 打赏
  • 举报
回复
先坐沙发

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧