【问题标题】:SQL issue with IN with union带有联合的 IN 的 SQL 问题
【发布时间】:2021-06-25 16:32:07
【问题描述】:

在子查询中与 Union 一起使用时,我无法让 In 过滤器工作。

SELECT
    CAST(table1.company_id__c as bigint),
    table1.casenumber
FROM 
    table2
LEFT JOIN 
    table1 ON table2.accountid = table1.id
WHERE
    table1.company_id__c IN (WITH t1 AS
                             (
                                 SELECT
                                     CAST(product_A_ID AS bigint)
                                 FROM
                                     table3
                                 WHERE
                                     filter_1 = 'Yes'
                                     AND product_A_ID IS NOT NULL
                             ),
                             t2 AS 
                             (
                                 SELECT
                                     CAST(product_B_ID AS bigint)
                                 FROM
                                     table3
                                 WHERE
                                     filter_1 = 'Yes'
                                     AND product_B_ID IS NOT NULL
                             ),
                             t3 AS 
                             (
                                 SELECT product_A_ID AS one_ID 
                                 FROM t1
                                 UNION
                                 SELECT product_B_ID AS one_ID 
                                 FROM t2
                             )
                             SELECT * 
                             FROM t3)
    AND table1.country__c = 'UK'
    AND table2.createddate BETWEEN '2020-01-01' AND '2021-12-31'

错误信息是

[代码:500310,SQL 状态:XX000] Amazon 无效操作:无效数字,值 'P',位置 0,类型:Long

详情:

错误:无效数字,值“P”,位置 0,类型:Long
代码:1207
上下文:PYMT-6247719908193229
查询:3738401

我可以让它工作 In 查询只是 1 个表(没有联合)

【问题讨论】:

    标签: sql amazon-redshift


    【解决方案1】:

    您不需要使用 WITH 语句,只需为 UNION 部分创建一个临时视图并从该临时视图中选择所需的列

    SELECT
        CAST(table1.company_id__c as bigint),
        table1.casenumber
    FROM 
        table2
    LEFT JOIN 
        table1 ON table2.accountid = table1.id
    WHERE 1=1
        AND table1.country__c = 'UK'
        AND table2.createddate BETWEEN '2020-01-01' AND '2021-12-31'
        and table1.company_id__c IN (
            select t3.one_id 
            from (
                (SELECT CAST(product_A_ID AS bigint) as one_id FROM table3 WHERE filter_1 = 'Yes' AND product_A_ID IS NOT NULL) 
                union all
                (SELECT CAST(product_B_ID AS bigint) one_id FROM table3 WHERE filter_1 = 'Yes' AND product_B_ID IS NOT NULL)
            ) t3
    

    【讨论】:

    • 不走运,同样的错误信息。 select cast(table1.company_id__c as bigint), table1.casenumber FROM table2 Left Join table1 on table2.accountid= table1.id where table1.company_id__c in ( select cast(product_A_ID as bigint) as ID2 from table3 where filter_1='Yes' and product_A_ID is not null UNION select cast(product_B_ID as bigint) as ID2 from table3 where filter_1='Yes' and product_B_ID is not null) and table1.country__c='UK' and table2.createddate between '2020-01-01' AND '2021-12-31'
    • 请检查编辑后的答案,看看它是否有效
    • 不走运,我认为 table1.company_id__c 字段有问题。下周我会仔细观察。不过感谢您的快速回复。
    猜你喜欢
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多