Sql 将两个空表作为泛型数据连接起来

Sql 将两个空表作为泛型数据连接起来,sql,sql-server-2008,Sql,Sql Server 2008,我有两个表,一个是事实数据(表1),另一个是第一个表(维度)的值的维度。 维度表使用null作为通用值 我知道我可以使用临时表和两个更新进行“连接”,但我认为一定有更好的方法。 我怎样才能加入 谢谢 Table1: Dimension: Col1: Code1 Code2: Code1: Code2: Value a 1 12

我有两个表,一个是事实数据(表1),另一个是第一个表(维度)的值的维度。 维度表使用null作为通用值

我知道我可以使用临时表和两个更新进行“连接”,但我认为一定有更好的方法。 我怎样才能加入

谢谢

       Table1:                                Dimension:
Col1:    Code1  Code2:                Code1:       Code2:    Value
a          1      12                    1           12         5 
b          1      15                    1           15         6
c          1      16                    1           16         7
d          1      17                    1         <null>       9 
e          1      20                   


       ResultTable                           
Col1:    Code1  Code2   Value                
a          1      12      5
b          1      15      6
c          1      16      7
d          1      17      9
e          1      20      9
表1:维度:
Col1:Code1 Code2:Code1:Code2:Value
A1215
B115156
C116167
d 1 17 1 9
e 120
可结
Col1:Code1 Code2值
a 1125
b 1156
C1167
d 1 17 9
e 1209

简化表格,这是一种方法,请查看

MS SQL Server 2017架构设置

create table a ( i int, a varchar(100) );
create table b ( i int );

insert into a values 
(1, 'A'),
(2, 'B'),
(Null, 'Generic');

insert into b values
(1),
(2),
(3),
(4);
with no_corresponding as
( select b.*
  from b left outer join a on a.i = b.i
  where a.i is null
)
select a.*, b.*
from a inner join b on a.i = b.i
union all
select a.*, no_corresponding.*
from no_corresponding cross join a
where a.i is null
|      i |       a | i |
|--------|---------|---|
|      1 |       A | 1 |
|      2 |       B | 2 |
| (null) | Generic | 3 |
| (null) | Generic | 4 |
查询

create table a ( i int, a varchar(100) );
create table b ( i int );

insert into a values 
(1, 'A'),
(2, 'B'),
(Null, 'Generic');

insert into b values
(1),
(2),
(3),
(4);
with no_corresponding as
( select b.*
  from b left outer join a on a.i = b.i
  where a.i is null
)
select a.*, b.*
from a inner join b on a.i = b.i
union all
select a.*, no_corresponding.*
from no_corresponding cross join a
where a.i is null
|      i |       a | i |
|--------|---------|---|
|      1 |       A | 1 |
|      2 |       B | 2 |
| (null) | Generic | 3 |
| (null) | Generic | 4 |

create table a ( i int, a varchar(100) );
create table b ( i int );

insert into a values 
(1, 'A'),
(2, 'B'),
(Null, 'Generic');

insert into b values
(1),
(2),
(3),
(4);
with no_corresponding as
( select b.*
  from b left outer join a on a.i = b.i
  where a.i is null
)
select a.*, b.*
from a inner join b on a.i = b.i
union all
select a.*, no_corresponding.*
from no_corresponding cross join a
where a.i is null
|      i |       a | i |
|--------|---------|---|
|      1 |       A | 1 |
|      2 |       B | 2 |
| (null) | Generic | 3 |
| (null) | Generic | 4 |

您需要一个默认值。您可以使用两个
left join
s:

select a.*, coalesce(b.value, bdefault.value) as value
from a left join
     b
     on a.code2 = b.code2 left join
     b bdefault
     on bdefault.code2 is null;
另一个有趣的方法是使用
外部应用

select a.*, b.value
from a outer apply
     (select top (1) b.value
      from b
      where b.code2 = a.code2 or b.code2 is null
      order by (case when b.code2 is not null then 1 else 2 end)
     ) b;

是否要包含或排除空值?我需要包含空值。但空值表示通用数据,即“维度中尚未包含的所有其他代码”是,假设维度表中只有一行
Null
。无论如何都应该是这样。请用你的否决票发表评论。(在我看来)这个答案没有错