![](/img/trans.png)
[英]how i can set null values in left join in presto with the max values from the right joined table
[英]SQL/Presto: right join bigger than original table due to NULL
我需要用 3 个条件右连接 2 个表,但结果表大于左表或右表。
left_table a 如下所示:
capacity value group_id level_id tags
100 3 a ab
120 5 a afb lala
122 4 b afg hhh
122 6 c adfg
右表 b 如下所示:大于左表
user group_id level_id tags
adsf a ab
af a abf df
sf a afb lala
dsf b afg hhh
sdf c adfg
我想把append的值和容量值放到右表b中。 我使用了以下查询,但结果表大于正确的表。 我注意到这是由于左右表中的标签中的 NULL,但我想知道如何解决这个问题。
select a.capacity, a.value, b.*
from a
right join b
on a.group_id = b._group_id
and a.level_id = b.level_id
and a.tags = b.tags
我注意到这是由于左右表中的标签中的 NULL
不,这不是重复的原因。 事实上NULL
值无法进行比较,因此如果任一值为NULL
,则根本不会匹配。 也就是说, b
中的行将返回 a 中列a
NULL
值。
如果您希望NULL
值匹配为相等,那么您需要一个NULL
-安全比较 - 并且 Presto 支持 SQL 标准与标准is not distinct from
。 我也非常喜欢left join
而不是right join
:
select a.capacity, a.value, b.*
from b left join
a
on a.group_id = b._group_id and
a.level_id = b.level_id and
a.tags is not distinct from b.tags;
如果你得到重复,那是因为你a
. 您可以使用以下方法检查:
select group_id, level_id, tags, count(*)
from a
group by group_id, level_id, tags
having count(*) >= 2;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.