我正在尝试将带有 OR 过滤器的 where 子句转换为 JOIN 子句,因为我收到错误

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

我在运行以下代码时遇到错误:

SELECT *
FROM m2_dataset_4 a --WHERE NVL(a.client,'0') NOT IN (SELECT NVL(customer,'0') FROM m1_dataset_5);

WHERE ((NVL(a.client, '0') NOT IN
          (SELECT DISTINCT NVL(client, '0')
           FROM m1_dataset_5))
       OR (NVL(a.client, '0') IN
             (SELECT DISTINCT NVL(client, '0')
              FROM m1_dataset_5
              WHERE hstatus = 'T'))
       OR (NVL(a.client, '0') IN
             (SELECT DISTINCT NVL(customer, '0')
              FROM m1_dataset_5
              WHERE client <> customer)));

错误信息是:

SQL 错误:org.apache.spark.sql.AnalysisException:空感知 谓词子查询不能在嵌套条件中使用:

请告诉我是否可以将 OR 子句转换为 JOIN 条件。我的主要议程是只选择:

  1. 不在客户端
  2. 已终止
  3. 非主要客户
sql thrift
1个回答
0
投票

您在 Spark SQL 中使用空感知谓词子查询,并且在嵌套条件中不支持它,因此您可能只需要使用 LEFT JOIN :

SELECT a.*
FROM m2_dataset_4 a
LEFT JOIN (
    SELECT DISTINCT NVL(client, '0') AS client
    FROM m1_dataset_5
) b1 ON NVL(a.client, '0') = b1.client

LEFT JOIN (
    SELECT DISTINCT NVL(client, '0') AS client
    FROM m1_dataset_5
    WHERE hstatus = 'T'
) b2 ON NVL(a.client, '0') = b2.client

LEFT JOIN (
    SELECT DISTINCT NVL(customer, '0') AS customer
    FROM m1_dataset_5
    WHERE NVL(client, '0') <> NVL(customer, '0')
) b3 ON NVL(a.client, '0') = b3.customer

WHERE b1.client IS NULL
   OR b2.client IS NOT NULL
   OR b3.customer IS NOT NULL
© www.soinside.com 2019 - 2024. All rights reserved.