Spark:相当于不在

Spark: Equivalent to not in

我在 Spark SQL 中有一个 where 子句,由于某种原因 return 没有任何记录。我认为它行不通,所以我想问一下与它等效的是什么?

SELECT
  c.client_id,
  current_date() as insert_date
FROM
  CLIENT_SUB c
WHERE
  (c.client_id, insert_date) not in (SELECT client_id, insert_date from CLIENT_SUBSCRIBER_CONTRACT)

我听说我可以通过连接来做到这一点

我怀疑您的子查询输出中有空值,因为 not in 在与包含空值的值匹配时不会输出任何内容。尝试

not in (select client_id, insert_date 
        from CLIENT_SUBSCRIBER_CONTRACT
        where coalesce(client_id, insert_date) is not null)

不过我还是建议您研究 not exists 以达到您的目的

我会推荐 not exists:它 null 是安全的,而 not it 不是 - 而且通常扩展性也更好。

我也对 insert_date 的引用表示怀疑:你是真的那个意思,还是你真的想要 current_date()

select cs.client_id, current_date() as insert_date
from client_sub cs
where not exists (
    select 1 
    from client_subscriber_contract csc
    where 
        csc.client_id = c.client_id 
        and csc.insert_date = cs.insert_date
        -- or, maybe: csc.insert_date = current_date()
)

为了提高性能,请考虑 client_subscriber_contract(client_id, insert_date) 上的索引。