Spark:相当于不在

问题描述

我在Spark SQL中有一个where子句,由于某种原因它不返回任何记录。我认为它不起作用,所以我想问一下这等同于什么?

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 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)上建立索引。

,

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

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

尽管如此,我还是建议您根据自己的意愿调查not exists

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...