Sql 如何获取过期金额和当前到期金额

Sql 如何获取过期金额和当前到期金额,sql,sql-server,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 Acc

我有两张表,即贷款和付款。我可以使用此脚本提取总费用:

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].[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日,则其逾期账户为103333.33,而当前到期账户为103333.33

案例2: 如果受托人能够在2020年2月28日支付其到期日的一半金额,并且达到2020年3月31日,则其逾期账户为51666.665,而当前到期账户为103333.33

案例3: 如果受托人能够在2020年2月28日到期日支付预付款,并且达到2020年3月31日,则其逾期账户为103333.33,而当前到期账户为51666.66

试试这个查询

    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]

我试着用我提到的不同的案例。为了尝试上面提到的第二个案例,我使用了以下数据:问题出在哪里?对于测试案例1:截止日期:2020年3月31日,显示了预期结果。然而,如果accountee决定提前询问他的会费,比如说2020年3月15日,它会显示以下结果。到期金额:206666.66逾期金额:103333.33测试用例2:结果到期金额:51666.67逾期金额:51666.67应到期金额:103333.33逾期金额:51666.67测试用例3:结果到期金额:-51666.66逾期金额:-51666.66应到期金额:51666.67逾期金额:0.00
    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]