2013-07-24 75 views
2

我想將3個表合併爲一個。我正在使用SQL Server 2005.我嘗試了FULL OUTER JOIN,但在結果中得到了重複的ID。所以你的幫助非常感謝。組合不同表中的列(SQL Server)

+---------------+ +---------------+ +---------------+ 
| 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富爾特,我完全連接查詢看起來是這樣的:

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子句添加一個額外層,以避免重複?

+0

? –

+0

@Francis Fuerte,當然,我將我的查詢添加到原始文章。 – myleslee

回答

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) 

基本上,做一個完全外部聯接的+ B,然後使用該結果加入到C.

+0

看起來像這樣更具防錯性。我唯一擔心的是演出。我上面給出的原型只涉及3個表格。實際上,有更多的表格加入,所以嵌套會變得更深。 – myleslee

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; 
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 

希望這有助於

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 

結果

enter image description here

+0

如果正在加入的表的順序發生變化,結果會發生變化: – myleslee

+0

如何?你能解釋一下嗎? – Nithesh

+0

當然,我已經把代碼片段和結果放在我原來的問題的上下文中。 – myleslee