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

求SQL语句 急!!!!!!!!!!!
有这样一个业务背景:
  业务员做收款计划单 单据上体现出来以下信息保存在table:t_plan(表头) t_planentry(表体)里面:
表头
  单据号:id
  客户号:fcust_id
  总金额:famount
  还款保证金:fkepamount
  月利息:ffax
表体:
  单据号:id 
  分录号:fentry
 应还时间:fdate 
 应还金额:fpay

  因为分期还款不但要每期气应还的款项,还要还每期的利息:每期还的利息=(累计剩余应还款金额-还款保证金)*月利息,所以要体现出来每期的要还的利息额,必须要在一张表里面体现出来累计剩余应还款金额.
  举一个例子:业务员录入一张单据如下:
  收款计划单
  单据号:num001 客户号: 001 总金额:1000 还款保证金:20 月利率:0.08%


  分录号 应还时间 应还金额
  1 2011-01-01 500
  2 2011-02-01 300
  3 2011-03-01 100
  4 2011-04-01 100
   

  也就是说,应收客户1000元 ,分四期收完,每期除了还应还的金额外,还要还利息,利息计算如上.那么这个时候需要一张计算累计剩余应还款金额的表体现出来,也就是根据以上信息,得到如下字段的表:'
  累计剩余应还款金额 分录号 应还时间 应还金额
  1000 1 2011-01-01 500 
  500 2 2011-02-01 300
  200 3 2011-03-01 100
  100 4 2011-04-01 100  
求指教

------解决方案--------------------
题目太长 建议短小一点
------解决方案--------------------
探讨
题目太长 建议短小一点

------解决方案--------------------
select d.id,d.fentry,d.fdate,d.fpay,h.famount-sum(isnull(d1.fpay,0)) as famount_Remain,d.fpay+(h.famount-sum(isnull(d1.fpay,0)))*h.ffax as fpay_Actual
from t_planentry d left join t_planentry d1 on d.id=d1.id and d.fdate>d1.fdate 
left join t_plan h on d.id=h.id
group by d.id,d.fentry,d.fdate,d.fpay,h.famount,h.ffax

是不是要这个结果?
------解决方案--------------------
select Totalfpay=sum(t2.fpay),t1.fentry,t1.fdate,t1.fpay
from t_planentry t1,
t_planentry t2
where t1.id=t2.id
and t1.fdate>=t2.fdate
and t1.id=@id
group by t1.fentry,t1.fdate,t1.fpay

------解决方案--------------------
SQL code

select p1.fentry,p1.fdate,p1.fpay,sum(p2.fpay) famount
from t_planentry p1 inner join
 (select fentry,fpay from t_planentry ) p2
on p1.fentry<=p2.fentry
group by p1.fentry,p1.fdate,p1.fpay

------解决方案--------------------
SQL code

create table tb_temp
(
id int,
date datetime,
mone int
)
insert into tb_temp
select 1,'2011-01-01',500 union all
select 2,'2011-02-01',300 union all
select 3,'2011-03-01',100 union all
select 4,'2011-04-01',100 

select 
calculate=(select sum (mone) from tb_temp b where b.id >= a.id)
,* from tb_temp a