在SQL中使用贷方付款日期和汇总获取借方的付款日期-慢查询

时间:2019-04-04 07:33:54

标签: sql tsql sql-server-2012

Edit2 :CTE是查询速度缓慢的原因。问题解决了。

修改:我将不创建更新,而是创建另一列付款日期。以下代码可以完美运行,但是速度非常慢。还有其他方法可以更快地做到这一点吗?

WITH sum_data AS (SELECT CustomerID
                   , ISSUEDATE
                   , DEBIT
                   , CREDIT
                   , SUM(DEBIT) over(PARTITION BY CustomerID 
                                     ORDER BY ISSUEDATE
                                     ROWS UNBOUNDED PRECEDING) SUM_DEBIT
                   , SUM(CREDIT) over(PARTITION BY CustomerID
                                      ORDER BY ISSUEDATE
                                      ROWS UNBOUNDED PRECEDING) SUM_CREDIT
               FROM Test_Table)
SELECT d.CustomerID
 , d.ISSUEDATE
 , d.DEBIT
 , d.CREDIT
 , (SELECT MIN(ISSUEDATE) 
               FROM sum_data d2
               WHERE d2.SUM_CREDIT >= d.SUM_DEBIT
                 AND d2.CustomerID  = d.CustomerID) PAYMENTDATE
FROM sum_data d
WHERE DEBIT != 0 

预编辑:我有一个显示不同客户交易信息的数据集。信用额度的发布日期和付款日期相同,因为它们是同时发行和付款的。但是,对于借方行,付款日期将是借方清算该借方的日期。因此,从底部开始,在输入贷项之前,我们有4个借方。 1398.83贷记清除了1336.13借方金额,因此我想用付款日期9/20/2018更新最后一行。其上方的2672信用额覆盖了1398.83的倒数第二行,因此该行的付款日期应为9/22/2018。 (1398.83-1336.13)+(2672-1398.83)的余额不覆盖剩余的借方,因此我们将继续检查是否有新的贷方行覆盖借方金额,并将从上一个项目开始清算借方(如更新付款日期)然后,如上例所示,当借方清算后,我们将使用清算贷方行的支付日期来更新支付日期。这将针对所有不同的客户ID(分区依据)完成。

CustomerID  ISSUEDATE   DEBIT   CREDIT  
M00008      4/2/2019    1345.53 0       
M00008      3/19/2019   0       1336.13 
M00008      3/18/2019   0       1403.4  
M00008      3/4/2019    1406.13 0       
M00008      3/4/2019    1336.13 0       
M00008      2/28/2019   0       1336.13 
M00008      2/21/2019   0       1399.19 
M00008      2/4/2019    1403.4  0       
M00008      2/4/2019    1336.13 0       
M00008      1/28/2019   0       1513    
M00008      1/22/2019   0       1337.94 
M00008      1/4/2019    1398.83 0       
M00008      1/4/2019    1336.13 0       
M00008      12/3/2018   1513.03 0       
M00008      12/3/2018   1336.13 0       
M00008      11/19/2018  0       2958    
M00008      11/19/2018  0       2908.25 
M00008      11/5/2018   1461.56 0       
M00008      11/2/2018   1367.92 0       
M00008      10/20/2018  0       1336.13 
M00008      10/19/2018  0       1398.83 
M00008      10/3/2018   1489.94 0       
M00008      10/2/2018   1541.03 0       
M00008      9/22/2018   0       2672    
M00008      9/20/2018   0       1398.83 
M00008      9/3/2018    1398.83 0       
M00008      9/3/2018    1336.13 0       
M00008      8/3/2018    1398.83 0       
M00008      8/2/2018    1336.13 0       

此数据集上方查询的输出:

CustomerID  ISSUEDATE   DEBIT    CREDIT PAYMENTDATE
M00008      2018-08-02  1336.13  0.00   2018-09-20
M00008      2018-08-03  1398.83  0.00   2018-09-22
M00008      2018-09-03  1398.83  0.00   2018-10-19
M00008      2018-09-03  1336.13  0.00   2018-10-20
M00008      2018-10-02  1541.03  0.00   2018-11-19
M00008      2018-10-03  1489.94  0.00   2018-11-19
M00008      2018-11-02  1367.92  0.00   2018-11-19
M00008      2018-11-05  1461.56  0.00   2018-11-19
M00008      2018-12-03  1513.03  0.00   2019-01-22
M00008      2018-12-03  1336.13  0.00   2019-01-28
M00008      2019-01-04  1398.83  0.00   2019-02-21
M00008      2019-01-04  1336.13  0.00   2019-02-21
M00008      2019-02-04  1403.40  0.00   2019-03-18
M00008      2019-02-04  1336.13  0.00   2019-03-18
M00008      2019-03-04  1406.13  0.00   NULL
M00008      2019-03-04  1336.13  0.00   NULL
M00008      2019-04-02  1345.53  0.00   NULL

1 个答案:

答案 0 :(得分:1)

在select语句中使用select始终会对性能产生影响。对于您的查询,使用CROSS APPLY应该会更快地产生结果。

WITH sum_data AS (SELECT CustomerID
               , ISSUEDATE
               , DEBIT
               , CREDIT
               , SUM(DEBIT) over(PARTITION BY CustomerID 
                                 ORDER BY ISSUEDATE
                                 ROWS UNBOUNDED PRECEDING) SUM_DEBIT
               , SUM(CREDIT) over(PARTITION BY CustomerID
                                  ORDER BY ISSUEDATE
                                  ROWS UNBOUNDED PRECEDING) SUM_CREDIT
           FROM Test_Table)
 SELECT d.CustomerID
      , d.ISSUEDATE
      , d.DEBIT
      , d.CREDIT
      , PaymentDate
   FROM sum_data d
   CROSS APPLY (SELECT MIN(ISSUEDATE) AS PaymentDate
           FROM sum_data d2
           WHERE d2.SUM_CREDIT >= d.SUM_DEBIT
             AND d2.CustomerID  = d.CustomerID) t
   WHERE DEBIT != 0