如何在 Redshift 中应用递归 CTE

问题描述 投票:0回答:1

这是我第一次在 Redshift 中使用递归 CTE。我想使用递归 CTE 获取数组中的所有值。这是我到目前为止的代码

WITH
flattened_data AS (
SELECT
    benefit_id,
    rule_value
FROM
    table1
),
array_elements AS (
SELECT
    benefit_id,
    TRIM(BOTH ' []"' FROM SPLIT_PART(rule_value, ',', 1)) AS element,
    1 AS position
FROM
    flattened_data
WHERE
    LENGTH(rule_value) > 2  -- Ensure the array is not empty

UNION ALL

SELECT
    ae.benefit_id,
    TRIM(BOTH ' []"' FROM SPLIT_PART(rule_value, ',', ae.position + 1)) AS element,
    ae.position + 1
FROM
    array_elements ae
JOIN
    flattened_data t1
ON
    ae.benefit_id = t1.benefit_id
WHERE
    TRIM(BOTH ' []"' FROM SPLIT_PART(t1.rule_value, ',', ae.position + 1)) IS NOT NULL
)
SELECT * FROM array_elements

但这总是会导致错误

ERROR: relation "array_elements" does not exist
。这是在 Redshift 中应用递归 CTE 的方法吗?

sql amazon-redshift
1个回答
0
投票

Redshift 无需递归即可完成此操作...即使您修复了它,只要数组中的字符串中包含嵌套逗号,您的代码也会中断。 您可以使用下面的代码来利用取消嵌套,但最好从一开始就将rule_value声明为super,以便复制命令正确导入数据而不是作为字符串。

SELECT benefit_id, element::varchar
from (SELECT benefit_id,
    json_parse(rule_value) rule_value
  FROM table1) t, t.rule_value element
© www.soinside.com 2019 - 2024. All rights reserved.