爲什麼你需要在一個time.Also 36000運行記錄是什麼actially安裝。對於任何學生只有一個分期付款沒有幾個。所以我認爲數據庫是錯誤的。 表中每個學生只能有一筆分期金額和時間間隔(截止日期)。
檢查最新的變化。它只適用於一個學生。
Declare @student table(SID int,SNAME varchar(50),INST Date ,Amount int)
insert into @student
select 123,'XYZ','01-01-2013',3500 union all
select 123, 'XYZ', '01-05-2013', 3500 union all
select 123, 'XYZ', '01-10-2013', 3500 union all
select 123, 'XYZ', '01-04-2014', 3500 union all
select 123, 'XYZ', '01-06-2014', 3500
Declare @instalmentamount float
select @instalmentamount=amount from @student where sid=123
--select @instalmentamount
Declare @Table2 table(SID int,Paydate date,amoount int)
insert into @Table2
select 123, '01-01-2013', 1167 union all
select 123, '01-02-2013', 1167 union all
select 123, '01-03-2013', 1167 union all
select 123, '01-05-2013', 1750 union all
select 123, '01-05-2013', 1750 union all
select 123, '01-10-2013', 1167 union all
select 123, '01-10-2013', 1167
declare @input date='01-01-2014'
;With CTE as
(select s.SID, sum(s.amount) as [Total Inst.Amt ] from @student s
where s.INST<[email protected]
group by s.SID),
cte1 as
(
select t.SID, sum(t.amoount) as [Paid.amount] from @Table2 t
where t.Paydate<[email protected]
group by t.SID
)
select c.sid,
(select top 1 s.SNAME from @student s where s.SID=c.SID) [Name],
c.[Total Inst.Amt ],c1.[Paid.amount],
c.[Total Inst.Amt ]-c1.[Paid.amount] [BalanceAmount],cast(((c.[Total Inst.Amt ]-c1.[Paid.amount])/@instalmentamount) as int)+1 [Balance.Installments]
from CTE c inner join cte1 c1 on c.SID=c1.SID
請發表你已經嘗試 –
尼斯家庭作業!你爲什麼不與我們分享你迄今爲止的嘗試? – gvee
「Balance.Installments」列代表什麼?在你的例子中它有'1'。你是如何達到樣本學生的這個價值的? – Shiva