问题描述
假设我有 2 个表:t1 和 t2,数据如下所示:
colA 和 colB 都是以字符串形式存储的。可以看到,colB 中的数据是用逗号分割的。
现在我想在 2 个表之间建立连接,条件是如果 colA 的值在用逗号分割后可以匹配 colB 的值。
我想代码可能看起来像:
patch_time
结果应该是这样的:
不知如何在Hive sql中达到同样的效果。我希望我的陈述是清楚的。如果您有任何想法,请帮助,谢谢!
解决方法
您应该修复数据模型。在字符串中存储多个值并不是存储数据的 SQL 方式。这些值应该在单独的行中。
如果您坚持使用其他人的非常非常糟糕的数据模型,您可以使用它,但查询效率远低于其他方式:
JsonDocument merchantSession;
if (!ModelState.IsValid ||
string.IsNullOrWhiteSpace(model?.ValidationUrl) ||
!Uri.TryCreate(model.ValidationUrl,UriKind.Absolute,out Uri requestUri))
{
return BadRequest();
}
// Create the JSON payload to POST to the Apple Pay merchant validation URL.
var request = new MerchantSessionRequest()
{
DisplayName = _options.StoreName,Initiative = "web",InitiativeContext = Request.GetTypedHeaders().Host.Value,MerchantIdentifier = _ApplePayServieBusiness.GetMerchantIdentifier(),};
merchantSession = await _ApplePayServieBusiness.GetMerchantSessionAsync(requestUri,request,cancellationToken);
,
你可以试试array_contains
:
select *
from t1
join t2
on array_contains(split(t2.colB,','),t1.colA)
,
select *
from t1
join (select t2.*
from t2 lateral view outer explode(split(t2.colB,')) e as colB_exploded
) t2
on t2.colB_exploded=t1.colA