【问题标题】:Automated Monthly data generation in SQL在 SQL 中自动生成每月数据
【发布时间】:2021-05-01 06:06:36
【问题描述】:

我是 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


在不破坏两个表之间的联合的情况下,我需要在哪里添加自动每月行

【问题讨论】:

  • 添加一个将所有日期截断到月初的联合(在 this question 的帮助下)。请更正您的模型描述和结果,因为:1)您的查询访问两个表,但您只提供了一个; 2)您的查询具有与您想要获得的结果不同的列,因此如何映射它们并不明显。
  • 如果上个月有预付款应该是什么结果?

标签: sql asp.net sql-server stored-procedures sql-server-2012


【解决方案1】:

伪代码:

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。

【讨论】:

  • davidtj 先生,您能翻译一下吗?我无法得到它。这对我很有帮助,因为我正处于学习阶段,而且我还是新手...如果你能翻译,会有很大帮助
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-23
  • 2010-11-01
  • 2011-09-11
  • 2015-06-16
  • 2013-10-15
  • 2018-12-06
  • 2021-02-28
相关资源
最近更新 更多