【问题标题】:SQL Left join on different columns considering specific conditions考虑特定条件的不同列上的 SQL 左连接
【发布时间】:2020-07-11 09:52:24
【问题描述】:

我相信这很容易。刚刚开始使用 SQL,所以我发现它有点棘手。所以我在 SAS 上使用 SQL,我想加入两个表,但基于列的值在不同的列上。实际例子:

Proc sql;
create table new_table_name as select 
a.proposal_code as new_name_proposal_code,
a.1st_client_code as new_name_1st_client_code,
a.2nd_client_code as new_name_2nd_client_code,
a.3rd_client_code as new_name_3rd_client_code,
a.4th_client_code as new_name_4th_client_code,
a.product_type as new_name_product_type,
b.2nd_client_code
from existing_table a
left join existing table b (on b.2nd_client_code=a.2nd_client_code and a.product_type = "clothes") or 
left join existing table b (on b.2nd_client_code=a.3rd_client_code and (a.product_type = "cars" or a.product_type = "bikes"));
quit;

所以这是我目前正在使用的代码,目标是使用 b.2nd 客户端代码 = a.2nd 客户端代码连接表 a 和表 b,如果表 a 中的产品类型为 = “衣服”,如果表 a 中的产品类型是“汽车”或“自行车”,则使用 b.2nd 客户端代码 = a.3rd 客户端代码连接表 a 和表 b。基本上,看一下关于特定产品类型的两个不同的“开”。加入这两个表时,如果一行有产品类型“衣服”,我希望它查看第二个客户端代码,如果是“汽车”或“自行车”,则查看第三个客户端代码。

希望我说清楚了。我目前遇到的错误是“期待开启”。是语法问题吗?

【问题讨论】:

  • 我不知道sas,但在sql中这是无效的:left join existing table b。此外,别名 b 应该是唯一的,第二个 left join 应该有另一个别名而不是第一个。
  • 听起来你只是想为 ON 标准评估一个复杂的表达式。除非您希望 A 和 B 中的相同两条记录在满足两个条件时产生两个不同的输出。
  • 您的变量名无效。 SAS(和大多数语言)中的名称不能以数字开头。
  • 您可以将案例添加到您的 SQL 语句中,包括连接。如果您发布一些示例数据和预期的输出,这将无限简单。
  • 两个 OR 条件是否可能同时为真?我怀疑他们可能是....

标签: sql sas left-join proc-sql


【解决方案1】:

是的。 on 之前的括号不正确。您的查询还有其他问题。我想你想要:

create table new_table_name as
    select a.proposal_code as new_name_proposal_code,
           a.1st_client_code as new_name_1st_client_code,
           a.2nd_client_code as new_name_2nd_client_code,
           a.3rd_client_code as new_name_3rd_client_code,
           a.4th_client_code as new_name_4th_client_code,
           a.product_type as new_name_product_type,
           coalsesce(bc.2nd_client_code, bcb.2nd_client_code)
    from existing_table a left join
         existing_table bc
         on bc.2nd_client_code = a.2nd_client_code and
            a.product_type = 'clothes' left join 
         existing_table bcb
         on bcb.2nd_client_code = a.3rd_client_code and
            a.product_type in ('cars', 'bikes');

注意事项:

  • on 子句之前没有括号。
  • 没有or left joinor 是一个布尔运算符。 left join 是集合(即表和结果集)上的运算符。请勿混用。
  • 没有重复的表别名。
  • 您想组合这两个代码,因此您需要 coalesce() 之类的 select
  • 字符串的 SQL 分隔符是单引号,而不是双引号。
  • inor 条件字符串简单。

【讨论】:

  • SAS 在字符串周围使用双引号或单引号,即使在 PROC SQL 中也是如此,除非您通过 PROC SQL 语句中的选项明确地告诉它不同。
  • @Gordon Linoff,bike 一词前缺少单引号。
  • @whymath 。 . .谢谢。
【解决方案2】:

听起来您只需要一个复杂的 ON 条件而不是两个连接。 像这样的:

proc sql;
create table new_table_name as 
select 
   a.proposal_code as new_name_proposal_code
  ,a.client_code1 as new_name_client_code1
  ,a.client_code2 as new_name_client_code2
  ,a.client_code3 as new_name_client_code3
  ,a.client_code4 as new_name_client_code4
  ,a.product_type as new_name_product_type
  ,b.client_code2 as new_name_other_client_code2
from tableA a
left join tableB b
  on (b.client_code2=a.client_code2 and a.product_type = "clothes")
  or (b.client_code2=a.client_code3 and a.product_type in ("cars","bikes"))
;
quit;

为了获得更好的答案,发布示例输入和所需的输出。

【讨论】:

    猜你喜欢
    • 2020-03-22
    • 2021-10-12
    • 1970-01-01
    • 2019-01-16
    • 2015-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多