字符串列表上的 BigQuery 左连接返回 null

Posted

技术标签:

【中文标题】字符串列表上的 BigQuery 左连接返回 null【英文标题】:BigQuery Left join on list of strings returns null 【发布时间】:2020-01-15 06:00:53 【问题描述】:

我想不出这个问题的好标题。对此感到抱歉。

从电子邮件集合中,我想检查哪些电子邮件来自未知用户列表。但是,当我像下面这样进行左连接时,连接似乎不匹配,而是在 tb2.email 中获得 NULL 值。

#StandardSQL 
with unknown_addresses AS (
  select 'test1@gmail.com' AS email
  union all
  select 'test2@gmail.com' AS email
  union all
  select 'test3@gmail.com' AS email
  union all
  select 'test4@gmail.com' AS email
  union all
  select 'test5@gmail.com' AS email
),

all_emails_received as (
  SELECT
    SUBSTR(REGEXP_EXTRACT(mystring, r"[a-z.@\-]+"), 4) as email,
    content
  FROM
    foobar 
)

SELECT tb1.email, tb1.content, tb2.email AS unknown_email 
FROM all_emails_received tb1 
  LEFT JOIN unknown_addresses tb2 
    ON tb1.email = tb2.email

如何使连接条件起作用?

我不想创建一个新表来存储未知地址。

(编辑)添加原始数据样本

| email           | content     | 
| --------------- | ----------- | 
| test1@gmail.com | Hello there | 
| gary@gmail.com  | test test   | 
| abc@gmail.com   | foobar      |  

我目前正在得到什么

| email           | content     | unknown_email   | 
| --------------- | ----------- | --------------- |
| test1@gmail.com | Hello there | null            |
| gary@gmail.com  | test test   | null            |   
| abc@gmail.com   | foobar      | null            | 

想要的结果

| email           | content     | unknown_email   | 
| --------------- | ----------- | --------------- |
| test1@gmail.com | Hello there | test1@gmail.com | 
| gary@gmail.com  | test test   | null            |   
| abc@gmail.com   | foobar      | null            | 

编辑 2:仍然无法正常工作。我在那里添加了 substr 和 regexp 过滤器。不确定这是否会影响我的结果。

【问题讨论】:

你能给我们一个样品tb1,以及一个样品想要的结果吗? 您的代码应该可以正常工作。你能举一些你的数据的例子吗? 为了重现您的错误并找出答案,我需要一段虚拟数据。你能提供一下吗? @angsty_robot,我很高兴能帮上忙。我将发布作为答案。如果您能接受答案并投票,我会很高兴。 @angsty_robot,我已经上传了答案。您能接受并投票赞成答案吗? 【参考方案1】:

我能够使用您的原始数据重现您的错误。我意识到问题出在你身上 REGEX EXP。因此,应该是:

REGEXP_EXTRACT(email, r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.+[a-zA-Z0-9-.]+$") as email

用上面那行代码修改一下,你应该可以得到想要的输出了。

很高兴能帮到你。

【讨论】:

以上是关于字符串列表上的 BigQuery 左连接返回 null的主要内容,如果未能解决你的问题,请参考以下文章

BigQuery 中的多个左连接

在 BIGQUERY 上使用 UNNEST 左连接

BigQuery 未在 LEFT JOIN 中返回缺失的 NULL 行

检查列表字符串 BigQuery 中的元素是不是

通过 BigQuery 上的更改事件聚合时间序列

SQL - 不等左加入 BigQuery