雪花中的相关子查询

时间:2020-10-15 22:33:42

标签: snowflake-schema snowflake-task

您能否让我知道如何将以下sql服务器代码转换为Snowflake,因为Snowflake不会 支持相关子查询。

SELECT DISTINCT CURRENT_DATE as creationdate, LastName, (SELECT Date AS Exp1 FROM PSU AS P WHERE (HQL = a.B1L) AND (HQH = a.B1H) AND (HQN = (SELECT MAX(HQN) AS Exp1 FROM PSU AS P2 WHERE (HQL = a.B1L) AND (HQH = a.B1H) AND (HQZ = 'P') AND (HQF = 'P'))))AS "paymentDATE", (CASE WHEN (SELECT MAX(H6P) FROM sss WHERE H6P = B1L AND H6CPS NOT IN ('H', 'C')) IS NULL THEN 'N' ELSE 'Y' END) AS "ORDER(Y/N)" FROM ACT AS a INNER JOIN
CU AS c ON c.BSL = a.B1L INNER JOIN CS AS cs ON cs.B2L = a.B1L AND a.B1H = cs.B2H WHERE (a.B1M <> 0) AND (cs.B2K IN ('H'))

我为上述代码创建的雪花代码,但尚未为“ ORDER(Y / N)”字段创建CTE:我觉得CTE中的联接不正确,并且不确定如何将CTE联接到main查询

WITH RECURSIVE PaymentDate AS (SELECT MAX(HQN) AS Expr1 FROM PSU AS P2 JOIN POP AS a ON (P2.HQL = a.B1L) AND (P2.HQH= a.B1H) AND (P2.HQZ = 'P') AND (P2.HQF= 'P') ) , LPD AS (SELECT Date as Exp1 FROM
PSU AS P JOIN ACT AS A ON (P.HQL = a.B1L) AND (P.HQH = a.B1H) JOIN TNR ON (P.HQN =TNR.Expr1) ) SELECT DISTINCT CURRENT_DATE as creationdate, LastName (CASE WHEN (SELECT MAX(H6P) FROM sss WHERE H6P = B1L AND H6CPS NOT IN ('H', 'C')) IS NULL THEN 'N' ELSE 'Y' END) AS "ORDER(Y/N)" FROM ACT AS a INNER JOIN CU AS c ON c.BSL = a.B1L INNER JOIN CS AS cs ON cs.B2L = a.B1L AND a.B1H = cs.B2H WHERE (a.B1M <> 0) AND (cs.B2K IN ('H'))

1 个答案:

答案 0 :(得分:0)

如果您将两个子查询都重写为应该工作的CTE。

第一个CTE获得以下值: HQL,HQH,MAX(HQN)

第二个CTE将PSU加入第一个CTE以获得“ paymentDATE”

将第二个CTE加入您的主要查询