SQL 查询使用两个表重叠总和,SQL 服务器

SQL Query to overlap the sums using two tables, SQL Server

如果有人能帮助我查询,我将不胜感激。

我有两个表 - PaymentsRepayments

Payments:                              Repayments:    
DATE         SUM      IS_REPAYED       DATE         SUM
01/01/15     20 000    0               05/01/15     5 000
                                       06/01/15     10 000

我需要一个标志,显示 Payment 总和是否被 Repayment 总和覆盖,在这种情况下,如果 (5 000 + 10 000) covers 20 000,现在不是。

但是添加新的Repayment时,是:

Payments:                              Repayments:    
DATE         SUM      IS_REPAYED       DATE         SUM
01/01/15     20 000    1               05/01/15     5 000
                                       06/01/15     10 000
                                       07/01/15     5 000

但是,也有这样的情况,当Repayments超过还款时,就意味着Payment已经还清,剩下的5 000 (25 000 - 20 000)应该算作Repayment对于 下一个 Payment,即:

Payments:                              Repayments:    
DATE         SUM      IS_REPAYED       DATE         SUM
01/01/15     20 000    1               05/01/15     25 000
01/02/15     10 000    0

当新的还款到来时:

Payments:                              Repayments:    
DATE         SUM      IS_REPAYED       DATE         SUM
01/01/15     20 000    1               05/01/15     25 000
01/02/15     10 000    1               05/02/15     5 000  (or more, ex. 6 000) then 1 000 goes to the next Payment

还有一种情况是Repayment在前,那么下一笔付款应该有一个标志:

Payments:                              Repayments:    
DATE         SUM      IS_REPAYED       DATE         SUM
                                       01/03/15     30 000
05/03/15     30 000     1              

那么,当我在某个时间查询表时,如何使用Payment/Repayment语句的当前情况来获得这个IS_REPAYED标志呢?我知道我可以在我的应用程序中为标志制定逻辑,这会是更好的解决方案吗?

要获得上面显示的视图,我使用 FULL OUTER JOIN:

SELECT t1.DATE, t1.SUM, NULL AS IS_REPAYED /*?*/, t2.DATE, t2.SUM
FROM 
(   (SELECT t1.*, row_number() OVER (ORDER BY DATE) as seqnum
    FROM Payments t1
    ) t1 
   FULL OUTER JOIN
   (SELECT t2.*, row_number() OVER (ORDER BY DATE) as seqnum
   FROM Repayments t2
   ) t2
ON t1.seqnum = t2.seqnum
)

嗯,我认为您可以通过以下 sql 查询来实现这一点

SELECT t1.DATE, SUM(t1.SUM), SUM(t1.SUM) = SUM(t2.SUM) AS IS_REPAYED, t2.DATE, SUM(t2.SUM)
FROM Payments t1 FULL OUTER JOIN Repayments t2 ON t1.DATE = t2.DATE
GROUP BY t1.DATE

1.当还款金额小于或等于当月还款金额时,将IS_REPAYED更新为PAYMENTStable中的0或1。

UPDATE PAYMENTS SET IS_REPAYED = S.IS_REPAYED
FROM
(
    SELECT P.[DATE],CASE WHEN  R.[SUM] = P.[SUM] THEN 1 ELSE 0 END IS_REPAYED
    FROM PAYMENTS P 
    JOIN 
    (
        SELECT CAST(DATEADD(month, DATEDIFF(month, 0, [DATE]), 0)AS DATE) [DATE],  SUM([SUM]) [SUM]
        FROM REPAYMENTS
        GROUP BY CAST(DATEADD(month, DATEDIFF(month, 0, [DATE]), 0)AS DATE)
    )R
    ON DATEPART(MONTH,P.[DATE]) = DATEPART(MONTH,R.[DATE]) AND R.[SUM] <= P.[SUM]
)S
WHERE PAYMENTS.[DATE]=S.[DATE]
  • Click here查看结果(还款金额小于还款金额)
  • Click here查看结果(还款金额等于还款金额)

2。在PAYMENTStable中将IS_REPAYED更新为0或1,并在还款金额大于该月的还款金额时插入两个table。

在内部查询中对每个月的数据进行求和和分组,然后与付款进行比较table。由于我们需要此数据进一步插入 PAYMENTSREPAYMENTS table,我们使用临时 table.

SELECT P.[DATE] PAYDATE ,P.[SUM] PAYSUM ,R.[DATE] REDATE,R.[SUM] RESUM
INTO #NEWTABLE
FROM PAYMENTS P 
JOIN 
(
    SELECT  CAST(DATEADD(month, DATEDIFF(month, 0, [DATE]), 0)AS DATE) [DATE],  SUM([SUM]) [SUM]
    FROM REPAYMENTS
    GROUP BY  CAST(DATEADD(month, DATEDIFF(month, 0, [DATE]), 0)AS DATE)
)R
ON DATEPART(MONTH,P.[DATE]) = DATEPART(MONTH,R.[DATE]) AND R.[SUM] > P.[SUM]

现在我们找出不同点插入到REPAYMENTStable

INSERT INTO REPAYMENTS
SELECT DATEADD(DAY,1,PAYDATE) [DATE], (RESUM-PAYSUM) REAMOUNT
FROM #NEWTABLE

同样我们找出不同点插入到PAYMENTS table

INSERT INTO PAYMENTS    
SELECT DATEADD(MONTH,1,PAYDATE) [DATE], (RESUM+(RESUM-PAYSUM))-PAYSUM REAMOUNT,1
FROM #NEWTABLE

由于 REPAYMENTS table 中的值大于 PAYMENTS table 我们直接将 table 的值从临时 table 更新为 1 .

UPDATE PAYMENTS SET ISREPAYED = 1
FROM
(
    SELECT PAYDATE FROM  #NEWTABLE
)N
WHERE PAYMENTS.[DATE]=N.PAYDATE