日期:2014-05-18  浏览次数:20729 次

分期收款的视图,如何建立?在线等
表:
stallid,costName,startdate,closedate,costtime,preday ,costprice
表数据如下
房号 费用类型 合同开始 合同截止 收费周期(月单位 ) 提前天数 金额
5F0001 租金 2007-01-01 2008-12-31 12 5 40000
5F0003 租金 2007-03-20 2008-11-19 6 5 20000
5F0004 租金 2007-05-08 2009-04-31 3 5 60000
5F0006 租金 2007-02-03 2007-08-02 2 5 40000
希望得到视图
stallid,costName,coststart,costclose,paydate,payvalue
房号 费用类型 费用开始 费用截止 应收日期 应收金额
5F0001 租金 2007-01-01 2007-12-31 2007-01-01 20000
5F0001 租金 2008-01-01 2008-12-31 2007-12-27 20000
5F0003 租金 2007-03-20 2007-09-19 2007-03-20 6000
5F0003 租金 2007-09-20 2008-03-19 2008-03-15 6000
5F0003 租金 2008-03-20 2008-09-19 2008-03-15 6000
5F0003 租金 2008-09-20 2008-11-19 2008-03-15 2000
......
依次类推,如何建立每期收款视图.
函数 用来计算两个日期相差月份,可以直接调用.
create function uf_GetMonthdiff(@startdate datetime,@closedate datetime)
returns int
as
begin
  declare @r int
  set @r=datediff(month,@startdate,@closedate)-1
  declare @s datetime
  set @s=dateadd(month,@r,@startdate)

  while dateadd(month,1,@s)<@closedate
  begin
  set @s=dateadd(month,1,@s)
  set @r=@r+1
  end
  if datediff(day,@s,@closedate)>15
  set @r=@r+1
  return @r
end

SELECT dbo.uf_GetMonthdiff('2007-03-20','2008-12-31') 得到21个月

------解决方案--------------------
SQL code
create table ta(房号 varchar(10),费用类型 varchar(6),
合同开始 datetime,合同截止 datetime,收费周期 int,提前天数 int,金额 int)
insert ta select
'5F0001','租金','2007-01-01','2008-12-31',12,5 ,            40000  union select 
 '5F0003','租金','2007-03-20','2008-11-19',6,5   ,          20000  union select 
 '5F0004','租金','2007-05-08','2009-04-30',3,5 , 60000  union select 
 '5F0006','租金','2007-02-03','2007-08-02',2,5     ,        40000 
go
select top 20 pid = identity(int,0,1) into # from sysobjects a,sysobjects b

select 房号 ,费用类型,
合同开始 = dateadd(mm,a.收费周期*pid,a.合同开始),
合同截止 = dateadd(mm,a.收费周期*(pid+1),a.合同开始),
应收日期 = case when pid = 0 then 合同开始 
                else dateadd(d,- 提前天数 ,dateadd(mm,a.收费周期*(pid+1),a.合同开始)) end

from ta a
left join # c on dateadd(mm,a.收费周期*pid,a.合同开始) < =a.合同截止

/*


房号         费用类型   合同开始                                                   合同截止                                                   应收日期                                                   
---------- ------ ------------------------------------------------------ ------------------------------------------------------ ------------------------------------------------------ 
5F0001     租金     2007-01-01 00:00:00.000                                2008-01-01 00:00:00.000                                2007-01-01 00:00:00.000
5F0001     租金     2008-01-01 00:00:00.000                                2009-01-01 00:00:00.000                                2008-12-27 00:00:00.000
5F0003     租金     2007-03-20 00:00:00.000                                2007-09-20 00:00:00.000                                2007-03-20 00:00:00.000
5F0003     租金     2007-09-20 00:00:00.000                                2008-03-20 00:00:00.000                                2008-03-15 00:00:00.000
5F0003     租金     2008-03-20 00:00:00.000                                2008-09-20 00:00:00.000                                2008-09-15 00:00:00.000
5F0003     租金     2008-09-20 00:00:00.000                                2009-03-20 00:00:00.000                                2009-03-15 00:00:00.000
5F0004     租金     2007-05-08 00:00:00.000                                2007-08-08 00:00:00.000                                2007-05-08 00:00:00.000
5F0004     租金