我要求编写MS-SQL存储过程,要求是
表1
它包括分期支付的学生总费用
EX:
SID SNAME INST.Date Amount
123 XYZ 01-01-2013 3500
123 XYZ 01-05-2013 3500
123 XYZ 01-10-2013 3500
123 XYZ 01-04-2014 3500
123 XYZ 01-06-2014 3500
`
表2
它包括付款recd详细信息
SID SNAME Paydate amoount
123 XYZ 01-01-2013 1167
123 XYZ 01-02-2013 1167
123 XYZ 01-03-2013 1167
123 XYZ 01-05-2013 1750
123 XYZ 01-05-2013 1750
123 XYZ 01-10-2013 1167
123 XYZ 01-10-2013 1167
现在的要求是2014年1月,支付了多少分期付款,什么是Recd金额,没有按照jan-14支付的余额分期付款。
预期输出
SID Sname Total Inst.Amt as on01.01.2014 Paid.amount Balance amt Balance.Installments
123 XYZ 10500 9335 1165 1
作为参考,我给了一个学生数据。
存储过程应该适用于36000名学生。
答案 0 :(得分:1)
这是一个可以满足您想要的问题,期待Balance.Installments
我不知道这意味着什么:
SELECT distinct t1.SID
, t1.SNAME
, t1.Total_Inst
, t2.Paid_amount AS 'Paid.Amount'
, t1.Total_Inst - t2.Paid_amount as 'Balance amt'
, CEILING( (t1.Total_Inst - t2.Paid_amount) / t.Amount ) as 'Balance.Installments'
FROM
table_1 as t
JOIN
( SELECT SID, SNAME, SUM(Amount) as Total_Inst
FROM table_1
WHERE Date < STR_TO_DATE('01/01/2014', '%m/%d/%Y') group by SID, SNAME
)
ON ( t1.SID = t.SID AND t1.SNAME = t.SNAME )
AS t1
LEFT JOIN
( SELECT SID, SNAME, SUM(Amount) as Paid_amount
FROM table_2
WHERE Paydate < STR_TO_DATE('01/01/2014', '%m/%d/%Y') group by SID, SNAME
) AS t2
ON ( t1.SID = t2.SID AND t1.SNAME = t2.SNAME )
答案 1 :(得分:0)
为什么你需要同时运行36000条记录。还有什么是可行的分期付款。对于没有几个学生,只有一个分期付款。所以我认为数据库是错误的。 表中每个学生应该只有一个分期金额和时间间隔(截止日期)。
检查最新的更改。这只适用于一名学生。
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<=@input
group by s.SID),
cte1 as
(
select t.SID, sum(t.amoount) as [Paid.amount] from @Table2 t
where t.Paydate<=@input
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
答案 2 :(得分:0)
尝试这个......
SELECT t.ID,
T.SNAME,
T.AMT 'Total Inst.Amt as on01.01.2014',
ISNULL(SUM(amoount),0) AS 'PAID AMT',
T.AMT - ISNULL(SUM(amoount),0) AS 'BALANCE AMT' ,
CEILING((T.AMT- ISNULL(SUM(amoount),0) )/( T.InstAmt*1.)) 'Balance.Installments'
FROM
(
SELECT ID,SNAME,SUM(AMT)AMT,MAX(AMT) AS InstAmt FROM @Table1 WHERE DT_ < '01-31-2014' GROUP BY ID,SNAME
) T
LEFT JOIN @Table2 P ON P.ID = T.id AND P.Paydate < '01-31-2014'
GROUP BY t.ID,SNAME,t.InstAmt,T.AMT