右加入后处理空值

问题描述

我有一个看起来像这样的表(table1):

| yyyy_mm_dd | xml_id | feature | status        |
|------------|--------|---------|---------------|
| 2020-07-10 | 2      | basic   | implemented   |
| 2020-07-10 | 2      | geo     | implemented   |
| 2020-07-10 | 2      | mobile  | first_contact |
| 2020-07-10 | 1      | geo     | first_contact |
| 2020-07-10 | 1      | mobile  | implemented   |
| 2020-07-10 | 3      | basic   | first_contact |
| 2020-07-10 | 3      | geo     | implemented   |

它基本上跟踪哪个xml_id实现了哪些功能。我还有第二个表,其中包含每个xml_id,它看起来像这样(table2):

| id     | name | active |
|--------|------|--------|
| 1      | xyz  | 1      |
| 2      | dfg  | 1      |
| 3      | lki  | 1      |
| 4      | nbg  | 0      |
| 5      | qyt  | 0      |
| 6      | bfh  | 1      |

请注意,xml_id是此处id的加入键。 table1包含ID的子集,而table2包含完整的列表。我想建立一个类似于第一个表的数据集,但具有一个名为no_contact的附加状态。如果每个功能的ID {尚未在id中,则表2中的no_contact的状态应为table1。如果需要,可以在table3中找到功能的完整列表:

| feature | metric          | app        |
|---------|-----------------|------------|
| basic   | basic_read      | promotions |
| basic   | basic_update    | promotions |
| basic   | basic_write     | promotions |
| geo     | geo_update      | admin      |
| geo     | geo_write       | admin      |
| mobile  | mobile_executed | admin      |

我已经尝试过了:

select
    t1.yyyy_mm_dd,t1.xml_id,t2.id,t1.feature,t1.implementation_status,case when t1.xml_id is null and t2.id is not null then 'no_contact' else t1.implementation_status end as implementation_status_2,from(
    select
        yyyy_mm_dd,xml_id,feature,implementation_status,from
        table1
) t1
right join
    table2
    on p.id = f.xml_id

但是,这给了我像这样的行

| t1.yyyy_mm_dd | t1.xml_id | p.id | t1.feature | t1.implementation_status | implementation_status_2 |
|---------------|-----------|------|------------|--------------------------|-------------------------|
| NULL          | NULL      | 4    | NULL       | NULL                     | no_contact              |
| NULL          | NULL      | 5    | NULL       | NULL                     | no_contact              |
| NULL          | NULL      | 6    | NULL       | NULL                     | no_contact              |

我可以看到逻辑是正确拾取第一个表中不存在的p_id,并分配正确的no_contact状态。但是,所有其他列均为空。我需要保留要素数据,以便我可以准确知道p_id与no_contact接触的要素。如果我使用left join,那么我将丢失p.id值,并且需要保留该值才能知道哪个id处于no_contact状态。如果可以为no_contactxml_id的{​​{1}}添加table1状态,这些状态没有状态,即xml_id = 1没有{{ 1}},所以我们应该为此添加一个basic行。

预期输出如下:

no_contact

示例架构,以及相同的提琴:

| yyyy_mm_dd | xml_id | feature | status        |
|------------|--------|---------|---------------|
| 2020-07-10 | 2      | basic   | implemented   |
| 2020-07-10 | 2      | geo     | implemented   |
| 2020-07-10 | 2      | mobile  | first_contact |
| 2020-07-10 | 1      | geo     | first_contact |
| 2020-07-10 | 1      | mobile  | implemented   |
| 2020-07-10 | 3      | basic   | first_contact |
| 2020-07-10 | 3      | geo     | implemented   |
| 2020-07-10 | 4      | mobile  | no_contact    |
| 2020-07-10 | 4      | geo     | no_contact    |
| 2020-07-10 | 4      | basic   | no_contact    |
| 2020-07-10 | 5      | mobile  | no_contact    |
| 2020-07-10 | 5      | geo     | no_contact    |
| 2020-07-10 | 5      | basic   | no_contact    |
| 2020-07-10 | 1      | basic   | no_contact    |
| 2020-07-10 | 3      | mobile  | no_contact    |

https://www.db-fiddle.com/f/6pPbW5TrR1HCL6so4kF5ev/0

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...