如何从 hive 列中的字符串集合中搜索字符串
我有以下两个输入表:表 1 和表 2
输入:表 1 id 和货币的类型为字符串
ID | 货币 |
---|---|
1 | USD、JPY、EUR |
2 | NOK、MXN |
3 | AUD |
4 | AUD、HKD |
输入:table2 exception_currency 的类型为字符串
exception_currency |
---|
澳元 |
挪威克朗 |
美元 |
港元 |
预期产出如下 如果表 1 中的每个货币列值与表 2 中的 exception_currency 不匹配,则异常为 yes。 例如,对于 id 1,例外为“YES”,因为表 2 中不提供 JPY 和 EUR。
ID | 货币 | 例外 |
---|---|---|
1 | USD,JPY,EUR | YES |
2 | NOK,MXN | YES |
3 | AUD | NO |
4 | AUD,HKD | NO |
我在下面尝试过代码,但没有得到预期的结果。
select
id,
currency,
case when array_contains(split(t1.currency,','), t2.exception_currency) then 'NO' else 'YES' as exception
from table1 t1 left join table2 t2 on (t1.currency=t2.exception_currency);
I have below two input table : table 1 and table 2
Input : table 1 id and currency are of type string
ID | Currency |
---|---|
1 | USD,JPY,EUR |
2 | NOK,MXN |
3 | AUD |
4 | AUD,HKD |
input: table2 exception_currency are of type string
exception_currency |
---|
AUD |
NOK |
USD |
HKD |
expected output as below
Exception is yes if the each currency column values from table 1 and exception_currency from table 2 are not matching.
For example for id 1, the exception is "YES" because JPY and EUR are not available in the table 2.
ID | Currency | Exception |
---|---|---|
1 | USD,JPY,EUR | YES |
2 | NOK,MXN | YES |
3 | AUD | NO |
4 | AUD,HKD | NO |
I tried below code but not getting expected results.
select
id,
currency,
case when array_contains(split(t1.currency,','), t2.exception_currency) then 'NO' else 'YES' as exception
from table1 t1 left join table2 t2 on (t1.currency=t2.exception_currency);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

首先将逗号分隔的值分成行,然后将其与 table2 进行比较。
这里横向视图将从逗号分隔值生成行。
将其与异常表连接以获得不存在的异常。
如果至少有一个值异常,最后 max 将显示 yes。
First seperate the comma separated values into rows and then compare it with table2.
Here lateral view will generate rows from comma separated values.
Joining it with exception table to get non existent exception.
Then finally max will show yes if at least one value is exception.