我创建了一个查询来生成cte表,两个是非递归的,一个是递归的,以便计算指数加权移动平均(ema)。当我在teradata中运行代码时,一段时间后它就会被tdwm杀死。
有什么办法来改进/解决这个问题吗?
WITH
smooth AS (
SELECT CAST(0.741870935 AS NUMERIC (20,5)) AS alpha
),
numbered AS (
SELECT
ROW_NUMBER() OVER (ORDER BY customer_name, closed_date) as rn,
customer,
closed_date,
metric
FROM my_table
),
recursive EWMA AS (
SELECT rn, customer, closed_date, metric, CAST(metric AS NUMERIC(20,5)) as EWMA
FROM numbered
WHERE rn = 1
UNION ALL
SELECT numbered.rn, numbered.customer, numbered.closed_date, numbered.metric,
smooth.alpha * numbered.metric + (1-smooth.alpha) * EWMA.EWMA
FROM EWMA
JOIN numbered
ON EWMA.rn + 1 = numbered.rn
CROSS JOIN smooth
)
SELECT * FROM EWMA
ORDER BY closed_date;
1条答案
按热度按时间uinbv5nw1#
你试过设定一个
depth
字段来限制递归?比如:递归连接
numbered
可能是问题的根源,假设my_table
table很大。理想情况下,您应该在pi列上执行一个直接的相等联接——即。table1.pi_col1 = table2.pi_col2
. 不知道如何使用+1
表达式将影响连接。从较高的层次来看,您似乎只想在当前行的计算中使用前一行的值。如果是这样的话,那么您可以完全取消递归cte,只使用
LAG()
窗口功能: