替换 table 中的值,然后将其连接到另一个 table

Replacing a value in a table and then joining it to another table

全新的网站,这是我的第一个问题!抱歉,我的 SQL 技能非常有限。我主要使用 Tableau,但需要在 Tableau 中创建自定义 SQL 连接。

我有 2 个 table(C table 和 P table)需要由 "Orders" 字段(在 tables) 和 "Prefix" 字段(仅在 C table 中找到,但希望更改 P table 中 "contract" 字段中的值以进行连接)。只有两个值需要更改。如果contract="1234",则改为"ABC. And it contract="5678",则改为"XYZ"。

更改这些值后,可以将来自 P table 的合同加入来自 C Table 的前缀。

抱歉,我无法更好地解释这一点,但正如我所说,我在 SQL 方面的经验非常有限。如有任何帮助,我们将不胜感激!

您可以使用 case 修改某些值的 contract。喜欢:

select  prefix + ' ' + case contract
        when '1234' then 'ABC'
        when '5678' then 'XYZ'
        else contract
        end as prefix_plus_contract
,       *
from    c
join    p
on      c.orders = p.orders

您可以使用子查询来完成。

select * 
    from C
    inner join (
        select *,
                case contract when 1234 then ABC
                              when 5678 then XYZ
                              else contract
                end as changed_contract -- or whatever name you like
            from P
    ) P on P.changed_contract = C.prefix
        and P.orders = C.orders

子查询中的 table 将为您提供 P 中的所有字段,包括一个新字段,该字段考虑了您所做的修改。然后你只需加入那个新字段并更改你的 select 语句以获取你想要的字段。