对不完整数据的完全外部联接(通过id变量)

时间:2015-04-09 16:54:41

标签: sql sql-server

我有两张桌子(见下面的示例数据)。我需要将所有ID值保留在table 1中,并将table 1table 2合并为sequence。棘手的部分是我还必须保留value1中的table 1字段和value2中的table 2字段。

table 1

    ID   sequence     value1    
    -------------------------
    p1      1         5      
    p1      2         10      
    p2      1         15 
    p2      2         20   

table 2

    sequence     value2    
    -------------------------
    1         10      
    2         20      
    3         30 
    4         40   

我需要生成的表格如下:

ID   sequence     value1    value2
----------------------------------
p1     1         5            10
p1     2         10           20
p1     3         -            30
p1     4         -            40
p2     1         15           10
p2     2         20           20
p2     3         -            30
p2     4         -            40

我尝试过以下sql代码,但它并没有合并value1table 1字段中的缺失值,而是将其与values2字段合并而来table 2 1}}

select t1.ID, t2.sequence, t1.value1, t2.value2 from
 t2 full outer join t1 on t2.sequence=t1.sequence

非常感谢您提供的任何帮助。

3 个答案:

答案 0 :(得分:3)

您可以尝试这样的事情:

select  coalesce(t1.[id], t3.[id]),
     ,  t2.[sequence]
     ,  t1.[value]
     ,  t2.[value]
from [tbl2] t2
left join [tbl1] t1 on t1.[sequence] = t2.[sequence]
left join (select distinct [id] from [tbl1]) t3 on t1.[id] is null

SQLFiddle

答案 1 :(得分:0)

CROSS JOINOUTER APPLY的一种方式:

DECLARE @t1 TABLE(ID CHAR(2), S INT, V1 INT)
DECLARE @t2 TABLE(S INT, V2 INT)

INSERT INTO @t1 VALUES
('p1', 1, 5),
('p1', 2, 10),
('p2', 1, 15),
('p2', 2, 20)


INSERT INTO @t2 VALUES
(1, 10),
(2, 20),
(3, 30),
(4, 40)


SELECT c.ID, t2.S, ca.V1, t2.V2 FROM @t2 t2
CROSS JOIN (SELECT DISTINCT ID FROM @t1) c
OUTER APPLY(SELECT * FROM @t1 t1 WHERE c.ID = t1.ID AND t1.S = t2.S) ca
ORDER BY c.ID, t2.S

输出:

ID  S   V1     V2
p1  1   5      10
p1  2   10     20
p1  3   NULL   30
p1  4   NULL   40
p2  1   15     10
p2  2   20     20
p2  3   NULL   30
p2  4   NULL   40

答案 2 :(得分:0)

鉴于此架构:

create table #table_1
(
  ID       varchar(8) not null ,
  sequence int        not null ,
  value    int        not null ,

  primary key clustered ( ID , sequence ) ,
  unique nonclustered   ( sequence , ID ) ,

)
create table #table_2
(
  sequence int not null ,
  value    int not null ,

  primary key clustered ( sequence ) ,

)
go

insert #table_1 values ( 'p1' , 1 ,  5 ) 
insert #table_1 values ( 'p1' , 2 ,  5 )
insert #table_1 values ( 'p2' , 1 , 15 )
insert #table_1 values ( 'p2' , 2 , 20 )

insert #table_2 values ( 1 , 10 )
insert #table_2 values ( 2 , 20 )
insert #table_2 values ( 3 , 30 )
insert #table_2 values ( 4 , 40 )
go

这可以让你得到你想要的东西:

select ID       = map.ID       ,
       sequence = map.sequence ,
       value1   = t1.value     ,
       value2   = t2.value
from ( select distinct
              t1.ID ,
              t2.sequence
       from       #table_1 t1
       cross join #table_2 t2
     ) map
left join #table_1 t1 on t1.ID       = map.ID
                     and t1.sequence = map.sequence
join #table_2      t2 on t2.sequence = map.sequence
order by map.ID ,
         map.sequence
go

产:

ID sequence value1 value2
== ======== ====== ======
p1      1      5     10
p1      2      5     20
p1      3      -     30
p1      4      -     40
p2      1     15     10
p2      2     20     20
p2      3      -     30
p2      4      -     40