【问题标题】:In Bigquery: How to fetch column value matching with other table but retaining the same column value if not matched在 Bigquery 中:如何获取与其他表匹配的列值,但如果不匹配则保留相同的列值
【发布时间】:2021-05-30 23:22:04
【问题描述】:

场景: 有两个具有相同列的 bigquery 表。必须比较这两个表w.r.t。类别和文章, i) 如果 table_2 中存在相同的内容,则必须从 table_2 中获取“Flow”列 ii) 否则,保留来自 Table_1 本身的相同 Flow 列。

表_1:

Category    Article     Flow
AA          11          Apple
AA          12          Orange
BB          13          Lemon
CC          14  

表_2:

Category    Article     Flow
AA          11          Melon
BB          13          Pine

结果表:

Category    Article     Flow
AA          11          Melon
AA          12          Orange
BB          13          Pine
CC          14

Tried_Out 查询:

select t1.Category, t1.Article, t2.Flow
from t1 left join t2
on t1.Category=t2.Category and t1.Article=t2.Article

帮我解决这个问题。提前致谢!

【问题讨论】:

    标签: join google-bigquery left-join case


    【解决方案1】:

    试试left join:

    with table_1 as (
      select 'AA' as category, 11 as article, 'Apple' as flow UNION ALL
      select 'AA', 12, 'Orange' UNION ALL
      select 'BB', 13, 'Lemon' UNION ALL
      select 'CC', 14, null
    ),
    table_2 as (
      select 'AA' as category, 11 as article, 'Melon' as flow UNION ALL
      select 'BB', 13, 'Pine'
    )
    select
      table_1.category,
      table_1.article,
      ifnull(table_2.flow, table_1.flow) as flow
    from table_1 left join table_2 using(category, article)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-03
      • 2015-02-11
      • 2016-06-19
      • 2020-05-18
      • 2019-01-20
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      相关资源
      最近更新 更多