第一个 table 的所有记录和第二个 table 的额外记录

All records from first table and extra records from second table

我有 2 个 table,我想在其中获取第一个 table 的所有记录和第二个 table 的额外记录。

Table一个

+-----+---------+---------+
| ID  | NAME    | TASK    |
+-----+---------+---------+
| 101 | Alan    | Prepare |
+-----+---------+---------+
| 102 | Fabien  | Approve |
+-----+---------+---------+
| 103 | Christy | Plan    |
+-----+---------+---------+
| 104 | David   | Approve |
+-----+---------+---------+
| 105 | Eric    | Set     |
+-----+---------+---------+

TableB

+-----+---------+---------+
| ID  | NAME    | TASK    |
+-----+---------+---------+
| 101 | Richy   | Prepare |
+-----+---------+---------+
| 103 | Girish  | Plan    |
+-----+---------+---------+
| 106 | Fleming | Approve |
+-----+---------+---------+
| 107 | Ian     | Set     |
+-----+---------+---------+

预期输出

+-----+---------+---------+
| ID  | NAME    | TASK    |
+-----+---------+---------+
| 101 | Alan    | Prepare |
+-----+---------+---------+
| 102 | Fabien  | Approve |
+-----+---------+---------+
| 103 | Christy | Plan    |
+-----+---------+---------+
| 104 | David   | Approve |
+-----+---------+---------+
| 105 | Eric    | Set     |
+-----+---------+---------+
| 106 | Fleming | Approve |
+-----+---------+---------+
| 107 | Ian     | Set     |
+-----+---------+---------+ 

我试过使用LEFT JOIN。但我只从左边 table.

select * from A left join B on A.ID=B.ID and B.ID is NULL

我也试过 UNIONUNION ALL 但由于 Name 在 2 table 中可能不同,所以我得到了这两个记录。一种解决方案可能是使用 NOT IN 但它对我来说很重要,因为我在这里将大查询称为 table A & B。我不知道我错过了什么。它应该很简单,但现在并没有引起我的注意。请帮忙。

我正在考虑在 ROW_NUMBER 和计算列的帮助下联合:

WITH cte AS (
    SELECT ID, NAME, TASK, 1 AS SRC FROM TableA
    UNION ALL
    SELECT ID, NAME, TASK, 2 FROM TableB
),
cte2 AS (
    SELECT *, ROW_NUMBER() OVER (PARTITION BY ID ORDER BY SRC) rn
    FROM cte
)

SELECT ID, NAME, TASK
FROM cte2
WHERE rn = 1;

这里的想法是构建一个包含来自两个 table 的所有记录的中间 table。我们引入了一个计算列来跟踪 table 源,并为 A 记录提供比 B 记录更高的优先级。使用 ROW_NUMBER 允许我们 select A 记录超过具有相同 ID.

的 B 记录

Full outer join 将起作用,因为完全连接将从两个表中获取所有匹配和不匹配的记录

;with tablea as 
(
select  101 as id,  'Alan'    name, 'Prepare ' as task
union select  102 , 'Fabien'  , 'Approve' 
union select  103 , 'Christy' , 'Plan    '
union select  104 , 'David'   , 'Approve '
union select  105 , 'Eric'    , 'Set   ')
,tableb as (
select  101 as ID  ,'Richy  ' as NAME   ,' Prepare ' as TASK     
union select  103 ,'Girish ',' Plan    '
union select  106 ,'Fleming',' Approve '
union select  107 ,'Ian    ',' Set '
)

select isnull(a.id,b.id) as id, isnull(a.name,b.name) as name, isnull(a.task,b.TASK) from tablea a
full outer join tableb b on a.id = b.ID

Result