【问题标题】:unmatched records should be pulled in the bigquery应该在 bigquery 中提取不匹配的记录
【发布时间】:2021-05-21 12:10:49
【问题描述】:

所需的结果集:

在下面的查询中,我正在获取按旧表和源表分组的记录,如果值匹配,则比较结果,然后我的标志列将填充匹配 但我希望输出中也有不匹配的记录(附件中的 HTC 产品不存在于源表中,因此该记录未填充)我的结果中也需要 HTC - 请帮助

查询框:

with legacy as (select product,sum(premium) as premium_l,reportdate
from Tab_legacy
group by product,reportdate),

source as (select product,sum(premium) as premium_s,reportdate
from Tab_src
group by product,reportdate)

select legacy.product,legacy.premium_l,source.premium_s,
case when legacy.premium_l = source.premium_s then 'match' else 'unmatch' end as match_flg,
round((legacy.premium_l - source.premium_s )/source.premium_s,2) as Diff,
source.reportdate
from legacy join source
on legacy.reportdate = source.reportdate
and legacy.product = source.product

【问题讨论】:

    标签: sql google-bigquery match


    【解决方案1】:

    你可以使用full join:

    with legacy as (
          select product, sum(premium) as premium_l, reportdate
          from Tab_legacy
          group by product, reportdate
         ),
         source as (
          select product, sum(premium) as premium_s, reportdate
          from Tab_src
          group by product, reportdate
         )
    select product, reportdate, legacy.premium_l, source.premium_s,
           (case when legacy.premium_l = source.premium_s
                 then 'match' else 'unmatch'
            end) as match_flg,
           round((legacy.premium_l - source.premium_s )/source.premium_s,2) as Diff,
    source.reportdate
    from legacy full join
         source
         using (reportdate, product);
    

    注意变化:

    • full join 而不是 join
    • using 而不是 on。这使得在select 中包含键值变得更容易(否则您需要coalesce() 逻辑)。
    • reportdateselect 中。它是join 的一部分,因此您应该包含它。

    【讨论】:

    • 以前的记录计数 4,但现在我看到 5 条记录,但我只看到第 5 条记录的所有列的空值
    • Product reportdate Legacy premium Source premium Match Flg Diff NULL NULL Null NULL Unmatch NULL 对于那个不匹配的记录我得到的只有 null 值没有得到产品名称作为 HTC 和相应的溢价值 - 请建议跨度>
    • @Ram。 . . all 列不应有NULL 值。特别是,前两个不应该是NULL——除非你的数据中有NULLs。
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多