如何从 hive 列中的字符串集合中搜索字符串

发布于 01-15 22:10 字数 1919 浏览 1 评论 0原文

我有以下两个输入表:表 1 和表 2

输入:表 1 id 和货币的类型为字符串

ID货币
1USD、JPY、EUR
2NOK、MXN
3AUD
4AUD、HKD

输入:table2 exception_currency 的类型为字符串

exception_currency
澳元
挪威克朗
美元
港元

预期产出如下 如果表 1 中的每个货币列值与表 2 中的 exception_currency 不匹配,则异常为 yes。 例如,对于 id 1,例外为“YES”,因为表 2 中不提供 JPY 和 EUR。

ID货币例外
1USD,JPY,EURYES
2NOK,MXNYES
3AUDNO
4AUD,HKDNO

我在下面尝试过代码,但没有得到预期的结果。

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

IDCurrency
1USD,JPY,EUR
2NOK,MXN
3AUD
4AUD,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.

IDCurrencyException
1USD,JPY,EURYES
2NOK,MXNYES
3AUDNO
4AUD,HKDNO

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

好倦2025-01-22 22:10:57

首先将逗号分隔的值分成行,然后将其与 table2 进行比较。

Select id, currency,
Max(excep) as exception 
from table1
Join (Select
Id, Case when exception_currency is null then 'yes' else 'no' end excep
From 
(SELECT ID, sep_curr
FROM table1 lateral view explode(split(currency,',')) currency AS sep_curr) rs
Left join table2 on exception_currency= sep_curr
) Rs on rs.id= table1.id
Group by id, currency

这里横向视图将从逗号分隔值生成行。

将其与异常表连接以获得不存在的异常。

如果至少有一个值异常,最后 max 将显示 yes。

First seperate the comma separated values into rows and then compare it with table2.

Select id, currency,
Max(excep) as exception 
from table1
Join (Select
Id, Case when exception_currency is null then 'yes' else 'no' end excep
From 
(SELECT ID, sep_curr
FROM table1 lateral view explode(split(currency,',')) currency AS sep_curr) rs
Left join table2 on exception_currency= sep_curr
) Rs on rs.id= table1.id
Group by id, currency

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文