根据不在单个记录中的查找表中的值从表中选择

时间:2018-10-04 17:32:21

标签: hive hiveql

我需要从表A中选择一个不在LKP表中的列值。查找表将在单个记录的列中存储值列表。

例如:

LKP的列C1在单个记录中的值为'INVALID','UNKNOWN',将具有类似的内容。

Table A:

ID
---
Bulbasaur
Charizard
Sqirtle
UNKNOWN
Ash
INVALID

Table LKP:

RULE    C1
----    ---
 1      'UNKNOWN','INVALID'

所需的以下代码输出:

select * from A where ID not in (select C1 from LKP where rule=1)

ID
---
Bulbasaur
Charizard
Sqirtle
Ash

我需要从table A中选择除C1中可用的单个记录之外的所有其他值。上述代码不起作用。我正在从A返回所有记录。是否应将C1的记录值插入LKP表中?如图所示,它必须在一条记录中以rule=1条件映射到where

请提出建议。

2 个答案:

答案 0 :(得分:0)

我不鼓励您将not in与子查询一起使用。如果任何返回值是NULL,则查询中将不返回任何行。

not exists是否可以解决您的问题?

select a.*
from A
where not exists (select 1 from LKP where lkp.rule = 1 and lkp.c1 = a.id);

答案 1 :(得分:0)

您可以splitexplode的csv字符串,然后进行比较。

with split_values as (select rule,c1,c1_split 
                      from LKP 
                      lateral view explode(split(c1)) tbl as c1_split
                     ) 
select * from a 
where not exists (select 1 from split_values s where s.c1_split=a.id and s.rule=1)
相关问题