Sql 与工会在不同的记录上联合

Sql 与工会在不同的记录上联合,sql,sql-server,join,Sql,Sql Server,Join,我有一个问题,尽管我做了很多努力,但还是无法解决。 我正在创建一个具有多个联接的表,并将从两个不同数据库中提取的表合并到一起。两个数据库之间的一些记录重叠,我有以下问题: 表1 表2: +------+--------+-------+-------+ | Col1 | Col2 | Col3 | Algo | +------+--------+-------+-------+ | 1 | jOB1 | Test1 | NULL | | 1 | JOB2 | Tes

我有一个问题,尽管我做了很多努力,但还是无法解决。 我正在创建一个具有多个联接的表,并将从两个不同数据库中提取的表合并到一起。两个数据库之间的一些记录重叠,我有以下问题: 表1

表2:

+------+--------+-------+-------+
| Col1 |  Col2  | Col3  | Algo  |
+------+--------+-------+-------+
|    1 | jOB1   | Test1 | NULL  |
|    1 |  JOB2  | Test2 | NULL  |
|    1 |  JOB3  | Test3 | ARFTU |
+------+--------+-------+-------+
联合

如何消除最终表中具有空值的行?如果另一个 行是否存在相同的Col1、Col2、Col3列

提前感谢

编辑我的最终结果以使其更加清晰: 最后我想得到的表是表2,也就是说,如果'Algo'值不存在,请将该行保留为null。如果“Algo”值确实存在,请删除空记录并仅保留填充的记录:

+------+--------+-------+-------+
| Col1 |  Col2  | Col3  | Algo  |
+------+--------+-------+-------+
|    1 | jOB1   | Test1 | NULL  |
|    1 |  JOB2  | Test2 | NULL  |
|    1 |  JOB3  | Test3 | ARFTU |
+------+--------+-------+-------+

为什么不直接使用聚合呢

select col1, col2, col3, max(algo) as algo
from ((select col1, col2, col3, algo from t1) union all
      (select col1, col2, col3, algo from t2)
     ) tt
group by col1, col2, col3;
试试这个

select * from (
  select * from t1 where algo is not null
  union
  select * from t2 where algo is not null)
t
我尝试先删除每个选择表。如果你有相同的col1,2,3值,并且algo有这两个值,它将被获取。
如果algo具有相同的值,则将合并,否则如果algo具有不同的值,则将同时获取这两个值。

计算应删除的值(下面的表间),然后 从结果中删除它们:

with both as (select * from t1 union select * from t2),
--- col1,col2,col3 for which one is null and the other is not
     inter as ((select col1, col2, col3 from t1 where algo is null 
               intersect
               select col1, col2, col3 from t2 where algo is not null) 
               UNION
               (select col1, col2, col3 from t1 where algo is not null 
               intersect
               select col1, col2, col3 from t2 where algo is null
select * from both
  except
-- remove those that are in the intersection but also have null
select * from 
    (select * from both where algo is null)  as temp2
    natural join inter;

谢谢你的建议!成功了!我希望有一些更优雅的东西(我的实际表有30多列),但仍然!谢谢@A.请将其标记为正确,然后关闭此。您能提供正确的结果吗?现在还不太清楚你是想消除所有的
NULL
s还是只消除
NULL
的JOB3。我想消除带有NULL的JOB3@Gordon Linoff答案就是我想要的答案
select * from (
  select * from t1 where algo is not null
  union
  select * from t2 where algo is not null)
t
with both as (select * from t1 union select * from t2),
--- col1,col2,col3 for which one is null and the other is not
     inter as ((select col1, col2, col3 from t1 where algo is null 
               intersect
               select col1, col2, col3 from t2 where algo is not null) 
               UNION
               (select col1, col2, col3 from t1 where algo is not null 
               intersect
               select col1, col2, col3 from t2 where algo is null
select * from both
  except
-- remove those that are in the intersection but also have null
select * from 
    (select * from both where algo is null)  as temp2
    natural join inter;