Sql 外键为空时,如何指向默认的伪记录
我有一个查询,在这个查询中我连接多维表来创建事实表。对于外键为null的情况,我希望将其指向维度表中添加的默认虚拟记录,以便字段中没有null值。这怎么可能。请帮忙Sql 外键为空时,如何指向默认的伪记录,sql,join,hive,foreign-keys,Sql,Join,Hive,Foreign Keys,我有一个查询,在这个查询中我连接多维表来创建事实表。对于外键为null的情况,我希望将其指向维度表中添加的默认虚拟记录,以便字段中没有null值。这怎么可能。请帮忙 Select a.name, a.id_no, d.dealer_name, d.address from contract a left join dealer b on a.con_id=b.con_id 对于上面的例子,当b.con_id为null,并且在这种情况下找不到匹配项时,我想指出在dealer表中添加的默认
Select
a.name,
a.id_no,
d.dealer_name,
d.address
from
contract a
left join dealer b
on a.con_id=b.con_id
对于上面的例子,当b.con_id为null,并且在这种情况下找不到匹配项时,我想指出在dealer表中添加的默认外键0。这就像如果FK为null,则使用默认的FK并使用这些记录填充字段。您可以将其保持为null,也可以将事实记录指向一个虚拟记录,显示没有引用,id<0表示它。您可以将其保持为null,也可以将事实记录指向一个显示没有引用的虚拟记录它的引用,id<0表示它。您可以在查询中使用ISNULL
例如:
您可以在查询中使用ISNULL
例如:
可以用默认值替换未联接的记录,如下所示:
Select
a.name,
a.id_no,
case when d.con_id is null then 'Default Name' else d.dealer_name end dealer_name ,
case when d.con_id is null then 'Default Address' else d.address end address
from
contract a
left join dealer d
on a.con_id=d.con_id
或使用con_id=0函数添加第二个连接:
Select
a.name,
a.id_no,
case when d.con_id is null then d0.dealer_name else d.dealer_name end dealer_name ,
case when d.con_id is null then d0.address else d.address end address
from
contract a
left join dealer d on a.con_id=d.con_id
left join dealer d0 on d.con_id is null and d0.con_id=0
可以用默认值替换未联接的记录,如下所示:
Select
a.name,
a.id_no,
case when d.con_id is null then 'Default Name' else d.dealer_name end dealer_name ,
case when d.con_id is null then 'Default Address' else d.address end address
from
contract a
left join dealer d
on a.con_id=d.con_id
或使用con_id=0函数添加第二个连接:
Select
a.name,
a.id_no,
case when d.con_id is null then d0.dealer_name else d.dealer_name end dealer_name ,
case when d.con_id is null then d0.address else d.address end address
from
contract a
left join dealer d on a.con_id=d.con_id
left join dealer d0 on d.con_id is null and d0.con_id=0
您可以采取确保每个维度表都有一个默认行的方法,例如使用-1作为值:
insert into dim_table (dim_table_id, . . . )
values (-1, . . . );
然后,在大多数数据库中,可以将引用声明为:
create table other_table (
. . .,
dim_table_id int not null default -1 references dim_table(dim_table_id)
. . .
);
但是,我认为配置单元还不支持列上的默认值。因此,您可以这样做,但在向引用表中插入新行时,需要显式插入-1。您可以采取确保每个维度表都有一个默认行的方法,例如使用-1作为值:
insert into dim_table (dim_table_id, . . . )
values (-1, . . . );
然后,在大多数数据库中,可以将引用声明为:
create table other_table (
. . .,
dim_table_id int not null default -1 references dim_table(dim_table_id)
. . .
);
但是,我认为配置单元还不支持列上的默认值。因此,可以这样做,但在向引用表中插入新行时,需要显式插入-1。是否需要使用伪记录?AFAIK如果列为null,则可以使用COALESCE指定值。是。但根据kimball dataware house design,这将是一种标准做法,从我所读到的内容来看,假设结果不应包含空值,因此coalesce将解决该问题。您的表契约是否有a.con_id=0的行?在这种情况下,您可以将连接条件设置为-on a.con_id=COALESCEb.con_id,0是否需要使用虚拟记录?AFAIK如果列为null,则可以使用COALESCE指定值。是。但根据kimball dataware house design,这将是一种标准做法,从我所读到的内容来看,假设结果不应包含空值,因此coalesce将解决该问题。您的表契约是否有a.con_id=0的行?在这种情况下,您可以将连接条件设置为-on a.con_id=COALESCEb.con_id,0小心,此解决方案将永远无法工作,因为在连接之前应用了ISNULL。你自己测试一下,你就会明白。请参阅我的测试:使用a作为select stack3,1,'val1',2,'val2',3,'val3'作为id,val,b作为select stack3,0,'val1',2,'val2',3,'val3'作为id,val select*从a.id=nvlb.id,0上的左连接b选择-这返回错误的结果。它将以这种方式工作小心,此解决方案将永远不会工作,因为在加入之前应用了ISNULL。你自己测试一下,你就会明白。请参阅我的测试:使用a作为select stack3,1,'val1',2,'val2',3,'val3'作为id,val,b作为select stack3,0,'val1',2,'val2',3,'val3'作为id,val select*从a.id=nvlb.id,0上的左连接b选择-这返回错误的结果。它将以这种方式工作