基于2列的Microsoft SQL Server条件联接

基于2列的Microsoft SQL Server条件联接,sql,sql-server,left-join,union,full-outer-join,Sql,Sql Server,Left Join,Union,Full Outer Join,我希望联接3个表,所有表都具有相同的数据,只是其中一列的名称不同(3个表中的每个表的日期不同)。这三张表如下所示。目标是如果表1和/或表2中存在条件,则确定表3中是否存在针对每个id/条件的条件。我现在将表2和表1连接起来,但我知道,如果表2中存在一个条件,而该条件不在表中,那么它将不被考虑,无论如何,任何帮助都将是有用的 Table 1 id place Condition_2018 123 ABC flu 456 ABC heart attack Tab

我希望联接3个表,所有表都具有相同的数据,只是其中一列的名称不同(3个表中的每个表的日期不同)。这三张表如下所示。目标是如果表1和/或表2中存在条件,则确定表3中是否存在针对每个id/条件的条件。我现在将表2和表1连接起来,但我知道,如果表2中存在一个条件,而该条件不在表中,那么它将不被考虑,无论如何,任何帮助都将是有用的

Table 1
    id  place Condition_2018 
    123  ABC  flu
    456  ABC  heart attack

Table 2
    id  place Condition_2019
    123  ABC  flu
    789  def  copd
Table 3
    id  place Condition_2020
    456  ABC  heart attack
    789  def  copd
    123  ABC  flu
OUTPUT:
Table 2
    id  place Condition_2018  Condition_2019  Condition_2020
    123  ABC  flu             flu             flu
    456  ABC  heart attack    null            heart attack
    789  def  NULL            copd            copd

谢谢大家!

如果您的数据库支持
完全连接
,您可以执行以下操作:

select
    id,
    place,
    t1.condition_2018,
    t2.condition_2019,
    t3.condition_2020
from table1 t1
full join table2 t2 using(id, place)
full join table3 t3 using(id, place)
否则,它就有点复杂了:
union all
,聚合是一种方法:

select 
    id, 
    place, 
    max(condition_2018) condition_2018,
    max(condition_2019) condition_2019,
    max(condition_2020) condition_2020
from (
    select id, place, condition_2018, null condition_2019, null condition 2020 from table1
    union all
    select id, place, null, condition_2019, null from table2
    select id, place, null, null, condition_2020 from table3
) t
group by id, place
这个(SQL Server语法)怎么样


您似乎希望表3中的所有内容与其他两个表中的内容相匹配。这就是左连接s:

select t3.id, t3.place,
       t1.condition_2018, t2.condition_2019, 
       t3.condition_2020
from table3 t3 left join
     table2 t2
     on t3.id = t2.id and t3.place = t2.place left join
     table1 t1
     on t3.id = t1.id and t3.place = t1.place;

您需要
表1
表2
完全外部联接
,以及
表3的
左联接

select 
  coalesce(t1.id, t2.id) id,
  coalesce(t1.place, t2.place) place,
  t1.Condition_2018,
  t2.Condition_2019,
  t3.Condition_2020
from table1 t1 full outer join table2 t2
on t2.id = t1.id
left join table3 t3
on t3.id = coalesce(t1.id, t2.id)
请参阅。
结果:


用您正在使用的数据库标记您的问题。@Brad。你的问题可能会更清楚。例如,如果条件每年都在变化,该怎么办?我同意表1和表2之间的完全联接,但它应该是表3的左联接(目标是表1或表2中是否存在条件,确定表3中是否存在针对每个id/条件的条件)。这是正确的,如果它出现在2018年和/或2019年,如果不为空,则根据条件进行匹配。谢谢,我做了一些修改,而不是就地加入t1。条件2018=t2。条件2019,在左侧加入x.condition_2018=t3。条件2020或x.condition_2019=t3.condition_2020,得到了我想要的。谢谢你的帮助和其他人。我喜欢你的加入;比我的答案更简洁。
select 
  coalesce(t1.id, t2.id) id,
  coalesce(t1.place, t2.place) place,
  t1.Condition_2018,
  t2.Condition_2019,
  t3.Condition_2020
from table1 t1 full outer join table2 t2
on t2.id = t1.id
left join table3 t3
on t3.id = coalesce(t1.id, t2.id)
>  id | place | Condition_2018 | Condition_2019 | Condition_2020
> --: | :---- | :------------- | :------------- | :-------------
> 123 | ABC   | flu            | flu            | flu           
> 456 | ABC   | heart attack   | null           | heart attack  
> 789 | def   | null           | copd           | copd