如何解决在 SQL 中自动生成每月数据
我是 ASP.NET 和 MSsql 的新手,我遇到了一个问题,我有一个房租支付系统,我想在每个月的月初自动生成一个像 Pay-Rent
这样的提醒我已经有房客在当月第一天后给房东的付房租和退还预付款的交易
EntryDate Transaction Advance Rent Paid Description Advanced
Return
2021-01-27 Rent Paid 0.00 2000.00 2k rent received 5000.00
2021-02-20 Rent Paid 0.00 9000.00 9000 rent paid 5000.00
2021-03-10 Rent Paid 0.00 100.00 100 rent paid 5000.00
2021-04-6 Rent Paid 0.00 99.00 99 amount paid 5000.00
2021-05-2 Advanced Money Returned 1000.00 0.00 1000 rent returned 5000.00
...我想做的是这个
EntryDate Transaction Rent Advance RentPaid Description Advanced
Return
2021-01-01 Please Pay your rent 5000 0 0 0 0
2021-01-27 Rent Paid 0 0.00 5000.00 5k rent received 5000.00
2021-02-01 Please Pay your rent 5000 0 0 0 0
2021-02-20 Rent Paid 0 0.00 5000.00 5k rent paid 5000.00
2021-03-01 Please Pay your rent 5000 0 0 0 0
2021-03-10 Rent Paid 0 0.00 5000.00 5k rent paid 5000.00
2021-04-01 Please Pay your rent 5000 0 0 0 0
2021-04-6 Rent Paid 0 0.00 5000.00 5k amount paid 5000.00
2021-05-01 Please Pay your rent 5000 0 0 0 0
2021-05-2 Advanced Money Returned 0 1000.00 5000.00 5k rent returned 5000.00
2021-01-01 Please Pay your rent 5000 0 0 0 0
这是我的 StoredProcedure 的代码
SELECT RR.EntryDate,(N'RentPaid') as TransactionName,0 as AdvanceReturn,RR.AmountPaid as RentPaid,RR.Description,MemberStartAmount as Advanced
FROM tblRentReceive as RR WHERE UserId = @UserId
UNION ALL
SELECT OGF.EntryDate,(N'Advanced Money Returned') as TransactionName,AR.Amount as Amount,0 as RentReceive,AR.Description,@MemberStartAmount as Advanced
FROM tblAdvancedReturn as AR WHERE AR.UserId = @UserId and AR.TenantId=@TenantId
ORDER BY EntryDate ASC
END
END
解决方法
伪代码:
START
for each record in master-record
begin
get the %MONTH% and %YEAR% of the record
create new record with day:1,month=%MONTH%,and year=%YEAR%,Transaction='Please pay your rent',and etc.
add the record and calculate the balance
end
add final-record 'Advanced Money Record' with the calculated balance
END
如果你有想法,你可以把它翻译成 tsql-stored-proc。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。