【问题标题】:SQL Join On Columns of Different LengthSQL 连接不同长度的列
【发布时间】:2019-09-27 12:57:00
【问题描述】:

我试图在 SQL 中将两个表连接在一起,其中列包含不同数量的唯一条目。

当我使用完全连接时,连接的列中的其他条目会丢失。

我使用的代码是(在 SAS proc SQL 中):

proc sql;
create table table3 as
select table1.*, table2.*
from table1 full join table2
on table1.id = table2.id;
quit;

问题的可视化示例(无法将实际表显示为包含敏感数据)

Table 1
  id  | count1
  1   |   2
  2   |   3
  3   |   2

Table 2
 id   | count2
  1   |   4
  2   |   5
  3   |   6
  4   |   2

Table 3
 id   | counta | countb
  1   |   2    |   4
  2   |   3    |   5
  3   |   2    |   6
  -   |   -    |   2     <----- I want don't want the id column to be blank in this row

我希望我已经足够清楚地解释了我的问题,提前感谢您的帮助。

【问题讨论】:

  • 只要选择coalesce(t1.id, t2.id),就会返回第一个非空的id值。
  • 请注意,您在此处发布的查询不会生成您发布的表 3 - 它会生成 4 列,而您发布的是 3
  • @CaiusJard 在 SAS 中,不能有两个同名的变量。因此,如果将该查询保存到数据集,则只会保存名为 ID 的第一列,而忽略其他副本。

标签: sql database join sas


【解决方案1】:

表 1 中的 id 为空,因为表 2 中的行在表 1 中没有匹配项。尝试查看此查询的输出:

select coalesce(table1.id, table2.id) as id, table1.count1, table2.count2
from table1 full join table2
on table1.id = table2.id;

Coalesce 从左到右工作,返回第一个非空值(它可以采用 2 个以上的参数)。如果表 1 中的 id 为 null,则使用表 2 中的 id

我还建议在查询中为所有表设置别名,所以我会这样写:

SELECT 
  COALESCE(t1.id, t2.id) as id, 
  t1.count1, 
  t2.count2
FROM 
  table1 t1 
  FULL OUTER JOIN 
  table2 t2
  ON 
    t1.id = t2.id;

【讨论】:

  • 那行得通,非常感谢。你能解释一下它是如何工作的吗?
【解决方案2】:

只需选择coalesce(t1.id, t2.id),将返回第一个非空id值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-03
    • 2011-12-12
    • 1970-01-01
    • 2016-08-18
    • 1970-01-01
    相关资源
    最近更新 更多