如何获取SQL Server中的过去到期金额和当前到期金额?

时间:2020-01-29 07:00:31

标签: sql sql-server

我有两个表,即“贷款”和“付款”。使用此脚本提取总费用没有问题:

SELECT SUM(x.Principal) - sum(amount_paid) 
FROM Payments
CROSS APPLY (SELECT SUM(Principal) as Principal 
             FROM Loans 
             WHERE AmortizationDate <= CONVERT(date, GETDATE()) 
               AND AccountNo = 2020001) AS x  
WHERE date_paid <= CONVERT(date, GETDATE()) 
  AND AccountNo =  = 2020001 

但是我想确定逾期金额(过去到期)和当前到期金额。

贷款:

CREATE TABLE [dbo].[Loans]
(  
    [RecID] [bigint] IDENTITY(1,1) NOT NULL,  
    [LoanApplicationId] [bigint] NULL,  
    [AccountNo] [nvarchar](50) NULL,  
    [LoanAmount] [decimal](16, 2) NULL,  
    [AmortizationDate] [date] NULL,  
    [Principal] [decimal](16, 2) NULL,  
CONSTRAINT [PK_Loans] PRIMARY KEY CLUSTERED   
( 
    [RecID] ASC  
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,   
ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]  
) ON [PRIMARY] 

付款:

CREATE TABLE [dbo].[Payments]
(
    [RecID] [bigint] IDENTITY(1,1) NOT NULL, 
    [receipt_no] [nvarchar](7) NOT NULL, 
    [AccountNo] [nvarchar](8) NOT NULL, 
    [date_paid] [datetime] NULL,
    [amount_paid] [money] NULL, 
 CONSTRAINT [PK_Payments] PRIMARY KEY CLUSTERED  
( 
    [RecID] ASC 
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,  
ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY] 
) ON [PRIMARY] 

样本贷款数据:

INSERT INTO Loans
           ([LoanApplicationId]
           ,[AccountNo]
           ,[LoanAmount]
           ,[AmortizationDate]
           ,[Principal])
     VALUES
           (2
           ,2020001
           ,310000.00
           ,'1/30/2019'
           ,103333.33
          )  
INSERT INTO Loans
           ([LoanApplicationId]
           ,[AccountNo]
           ,[LoanAmount]
           ,[AmortizationDate]
           ,[Principal])
     VALUES
           (2
           ,2020001
           ,310000.00
           ,'2/28/2020'
           ,103333.33
          )  
INSERT INTO Loans
           ([LoanApplicationId]
           ,[AccountNo]
           ,[LoanAmount]
           ,[AmortizationDate]
           ,[Principal])
     VALUES
           (2
           ,2020001
           ,310000.00
           ,'3/31/2020'
           ,103333.33
          )

样本付款数据:

 INSERT INTO Payments
               ([receipt_no]
               ,[AccountNo]
               ,[date_paid]
               ,[amount_paid])
         VALUES
               (1
               ,2020001
               ,'1/30/2020'
               ,103333.33)

我想生成这样的东西:

情况1: 如果收款人无法在2020年2月28日付款,而到2020年3月31日,则他的逾期帐户为103,333.33,而当期应付款为103,333.33。

情况2: 如果受款人可以在到期日的2020年2月28日支付一半金额并达到2020年3月31日,则他的逾期帐户为51,666.665,而当期应付款为103,333.33。

情况3: 如果收款人可以预付款项于2020年2月28日支付预付款并达到2020年3月31日,则其逾期帐户为103,333.33,而当期应付款为51,666.66。

1 个答案:

答案 0 :(得分:0)

尝试此查询

    declare @dueamount money,@overdueamount money

    set @dueamount=

    (
    (select sum(principal)
    from
    (select  *
     from #loans
    where AmortizationDate<getdate()
    union
    select  
    top 1 * from #loans
    where AmortizationDate>getdate())A)
    -
    (select sum(amount_paid) from #Payments))




    set @overdueamount=
    (
    (
    select  SUM(Principal)
     from #loans
    where AmortizationDate<getdate()
    )
    -
    (select sum(amount_paid) from #Payments))

    select @dueamount as [Due Amount], @overdueamount as [OverDue Amount]