2

我想将 3 张桌子合二为一。我正在使用 SQL Server 2005。我尝试过FULL OUTER JOIN,但结果中出现了重复的 ID。因此,非常感谢您的帮助。

+---------------+   +---------------+    +---------------+ 
| ID   COL_A    |   | ID    COL_B   |    | ID    COL_C   |
+---------------+   +---------------+    +---------------+
| a    2        |   | b     1       |    | a     1       |
| c    1        |   | c     1       |    | d     1       |
+---------------+   +---------------+    +---------------+

结果:

+---------------------------+
| ID   COL_A  COL_B  COL_C  |
+---------------------------+   
| a    2      NULL   1      | 
| b    NULL   1      NULL   |
| c    1      1      NULL   |
| d    NULL   NULL   1      |
+---------------------------+

每个表都有不同的数据行。

这是创建表的代码:

declare @a table (
    ID      char(1),
    COL_A   int
)
declare @b table (
    ID      char(1),
    COL_B   int
)
declare @c table (
    ID      char(1),
    COL_C   int
)

insert into @a values ('a', 2)
insert into @a values ('c', 1)

insert into @b values ('b', 1)
insert into @b values ('c', 1)

insert into @c values ('a', 1)
insert into @c values ('d', 1)

select * from @a
select * from @b
select * from @c

谢谢!


@Francis Fuerte,我的全连接查询如下所示:

SELECT  COALESCE(c.ID,a.ID,b.ID) AS ID
       ,a.COL_A
       ,b.COL_B
       ,c.COL_C 
FROM @c c 
FULL OUTER JOIN @a a ON c.ID=a.ID
FULL OUTER JOIN @b b ON c.ID=b.ID  
ORDER BY ID

@Nithesh,如果加入的表的顺序发生变化,结果也会发生变化。

SELECT  ISNULL(ISNULL(a.ID,b.ID),c.ID) AS ID
       ,a.COL_A
       ,b.COL_B
       ,c.COL_C 
FROM @a a 
FULL OUTER JOIN @b b ON a.ID=b.ID  
FULL OUTER JOIN @c c ON a.ID=c.ID
ORDER BY ID

SELECT  ISNULL(ISNULL(b.ID,a.ID),c.ID) AS ID
       ,a.COL_A
       ,b.COL_B
       ,c.COL_C 
FROM @b b 
FULL OUTER JOIN @a a ON b.ID=a.ID  
FULL OUTER JOIN @c c ON b.ID=c.ID
ORDER BY ID

SELECT  ISNULL(ISNULL(c.ID,b.ID),a.ID) AS ID
       ,a.COL_A
       ,b.COL_B
       ,c.COL_C 
FROM @c c 
FULL OUTER JOIN @b b ON c.ID=b.ID  
FULL OUTER JOIN @a a ON c.ID=a.ID
ORDER BY ID

结果:(对不起,没有足够的声誉来发布图片)

+---------------------------+
| ID   COL_A  COL_B  COL_C  |
+---------------------------+   
| a    2      NULL   1      | 
| b    NULL   1      NULL   |
| c    1      1      NULL   |
| d    NULL   NULL   1      |
+---------------------------+
+---------------------------+
| ID   COL_A  COL_B  COL_C  |
+---------------------------+   
| a    2      NULL   NULL   | 
| a    NULL   NULL   1      |
| b    NULL   1      NULL   |
| c    1      1      NULL   |
| d    NULL   NULL   1      |
+---------------------------+
+---------------------------+
| ID   COL_A  COL_B  COL_C  |
+---------------------------+   
| a    2      NULL   1      | 
| b    NULL   1      NULL   |
| c    NULL   1      NULL   |
| c    1      NULL   NULL   |
| d    NULL   NULL   1      |
+---------------------------+

我真的需要添加一个带有 GROUPBY 子句的额外层以避免重复吗?

4

5 回答 5

2

鉴于您的样本,以下将起作用:

SELECT ISNULL(AB.ID, C.ID) AS [ID], AB.COL_A, AB.COL_B, C.COL_C
FROM (
    SELECT ISNULL(A.ID, B.ID) AS [ID], A.COL_A, B.COL_B
    FROM @a A
    FULL OUTER JOIN @b B
        ON A.ID = B.ID
) AB
FULL OUTER JOIN @c C
    ON AB.ID = C.ID
ORDER BY ISNULL(AB.ID, C.ID)

基本上,做一个 A+B 的 FULL OUTER JOIN,然后使用它的结果加入 C。

于 2013-07-24T02:37:29.287 回答
0

这是一篇关于连接类型的精彩文章,有助于通读。

http://blog.sqlauthority.com/2009/04/13/sql-server-introduction-to-joins-basic-of-joins/

假设您想保留数据来源的身份。A 栏,B 栏,C 栏

不确定您对返回的期望是什么,但据我所知,您应该更改为表之间的内部联接。请记住,您一次只能加入一张桌子。

于 2013-07-24T02:38:28.730 回答
0
select  case when a.ID IS not null then a.ID 
        when b.ID IS not null then b.ID 
        when c.ID IS not null then c.ID 
    end id
    ,a.COL_A
    ,b.COL_B 
    ,c.COL_C
from    @a a full outer join @b b on a.ID = b.id
         full outer join @c c on a.ID = c.id
order by 1

希望这可以帮助

于 2013-07-24T02:47:53.690 回答
0

最简单的方法是使用union all和聚合:

select id, max(Col_A) as Col_A, max(Col_B) as Col_B, max(Col_C) as Col_c
from ((select id, col_A, NULL as col_B, NULL as COL_C
       from @a
      ) union all
      (select id, NULL, col_B, NULL
       from @b
      ) union all
      (select id, NULL, NULL, COL_C
       from @c
      )
     ) abc
group by id;
于 2013-07-24T02:35:55.220 回答
0

尝试这个

SELECT  ISNULL(ISNULL(a.ID,b.ID),c.ID) AS ID
       ,a.COL_A
       ,b.COL_B
       ,c.COL_C 
FROM @a a 
FULL OUTER JOIN @b b ON a.ID=b.ID  
FULL OUTER JOIN @c c ON a.ID=c.ID
ORDER BY ID

结果

在此处输入图像描述

于 2013-07-24T06:45:07.893 回答