将 OUTER APPLY 转换为 Redshift

Convert OUTER APPLY to Redshift

我有 MSSQL Server 脚本需要转换为 Redshift

这是脚本的一部分

 OUTER APPLY
        (
        SELECT  MAX(CASE WHEN ctt.codetagid = 7149 THEN Id ELSE 0 END) AS CslId,
                MAX(CASE WHEN ctt.codetagid = 9736 THEN Id ELSE 0 END) AS LearnId,
                MAX(CASE WHEN ctt.codetagid = 9735 THEN Id ELSE 0 END) AS PMId,
                MAX(CASE WHEN ctt.codetagid = 10299 THEN Id ELSE 0 END) AS UpSell
        FROM    public.billing_codes_custom_tag ctt 
        WHERE   ctt.customcodeid = bcc.id
                AND ctt.codetagid IN(7149,9736,9735,10299)
        ) AS codeLabel

我有错误

'(', ',', , , , AT, CROSS, EXCEPT, FETCH, FULL, GROUP, HAVING, INNER, INTERSECT, ISNULL, JOIN, LEFT, LIMIT, MINUS, NATURAL, NOTNULL, OFFSET, OPERATOR, ORDER, RIGHT, UNION, WHERE or '[' expected, got 'OUTER'

在这一行OUTER APPLY

如何将这些东西转换为 Redshift?

因为 OUTER APPLY 与 LEFT JOIN 相同

我这样重写

LEFT JOIN
    (
    SELECT  MAX(CASE WHEN ctt.codetagid = 7149 THEN Id ELSE 0 END) AS CslId,
            MAX(CASE WHEN ctt.codetagid = 9736 THEN Id ELSE 0 END) AS LearnId,
            MAX(CASE WHEN ctt.codetagid = 9735 THEN Id ELSE 0 END) AS PMId,
            MAX(CASE WHEN ctt.codetagid = 10299 THEN Id ELSE 0 END) AS UpSell
    FROM    public.billing_codes_custom_tag ctt 
    WHERE   ctt.customcodeid = bcc.id
            AND ctt.codetagid IN(7149,9736,9735,10299)
    ) AS codeLabel

您将使用 LEFT JOIN 和聚合:

LEFT JOIN
    (SELECT ctt.customcodeid, 
            MAX(CASE WHEN ctt.codetagid = 7149 THEN Id ELSE 0 END) AS CslId,
            MAX(CASE WHEN ctt.codetagid = 9736 THEN Id ELSE 0 END) AS LearnId,
            MAX(CASE WHEN ctt.codetagid = 9735 THEN Id ELSE 0 END) AS PMId,
            MAX(CASE WHEN ctt.codetagid = 10299 THEN Id ELSE 0 END) AS UpSell
     FROM public.billing_codes_custom_tag ctt 
     WHERE ctt.codetagid IN (7149, 9736, 9735, 10299)
     GROUP BY ctt.customcodeid
    ) codeLabel
    ON codeLabel.customcodeid = bcc.id