如何解决进行左连接时,在右表上标记所有缺失值的记录
我有两个表,分别代表不同年份 Left Table: 2019
和 Right Table: 2018
的客户数据。其中一些客户在前一年不存在,我的假设是在进行左连接时,这些客户的右表中的所有字段值都将丢失。有没有办法标记这些客户(或记录)?以下是我的查询
create table joinedTable as
select a.unique_number,a.monthly_spend,b.unique_number,b.email_preference,b.client_tier,from table2019 as a
left join table2018 as b
on a.unique_number = b.unique_number
以下是所需的输出。
unique_number | monthly_spend | email_prefernce | client_tier | 旗帜 |
---|---|---|---|---|
12AB56FG | 2000 | 是 | 特殊 | 0 |
32AB56FG | 1200 | 是 | 特殊 | 0 |
42AB56FG | 2010 | 是 | 特殊 | 0 |
56AB56HG | 3000 | 是 | 特殊 | 0 |
72AB58FG | 6000 | NULL | NULL | 1 |
92AB56FG | 800 | NULL | NULL | 1 |
解决方法
只需使用 case
表达式:
create table joinedTable as
select a.unique_number,a.monthly_spend,b.unique_number,b.email_preference,b.client_tier,(case when b.unique_number is null then 1 else 0 end) as flag
from table2019 a left join
table2018 b
on a.unique_number = b.unique_number;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。