如何使用Teradata SQL执行未来的现金流量预测?

时间:2019-06-11 15:42:50

标签: sql teradata

我正在努力使用SQL获得未来的现金流量。它包括未偿余额,标准付款,利息和本金。目前,我在excel中有报告,但是我需要在Teradata中获得完全相同的内容。

我想将我的问题分为两部分:

  1. 从表格中获取当前余额和利率(很容易)

  2. 生成未来20天的未来日期和项目(不确定如何执行此操作)

到目前为止,我都尝试过Windows函数,但无法执行。

SELECT
   "Account Number"
   ,"Business Date"
   ,"Outstanding Balance"
   ,"Standard Payment" 
   ,"Current Balance" - "Standard Payment" "Dummy Balance" 
   ,MAX("Dummy Balance" ) OVER (PARTITION BY "Account Number" ORDER BY "Business    Date" ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING  )"Prev Balance"
FROM table
  where [<Conditions>]
  GROUP BY 1,2,3,4
  ORDER BY "NEXT DAYS";

enter image description here

如图所示,期末余额返回到期初余额字段,并根据此新的期初余额值计算利息和本金。

例如,第一行的期初余额值为100,利息和本金以100为基础计算,然后从期初余额中减去,得出期末余额为99.70200384。在下一行中,这将成为期初余额,并根据此余额计算所有其他值。

是否可以在SQL中完成?

1 个答案:

答案 0 :(得分:0)

您需要一些递归SQL。假设您每个帐户有一行,并希望在接下来的10天中获得该行:

WITH RECURSIVE cte AS
 (
   SELECT 
      account_number,
      Cast(100 AS DECIMAL(38,8)) AS balance,
      0.56466283 AS payment,
      0.00266666667 AS interest_rate,
      balance * interest_rate AS interest,
      payment - interest AS principal,
      balance - principal AS end_balance,
      Current_Date AS dt
   FROM mytable

   UNION ALL

   SELECT
      account_number,
      end_balance,
      payment,
      interest_rate,
      end_balance * interest_rate AS new_interest,
      payment - new_interest AS new_principal,
      end_balance - new_principal AS new_balance,
      dt+1 as new_dt
   FROM cte
   WHERE new_dt < Current_Date+10
 )
SELECT * 
FROM cte 
ORDER BY account_number, dt