正在尝试编写有问题的sql联接

kwvwclae  于 2021-07-29  发布在  Java
关注(0)|答案(1)|浏览(322)

表opp

OppA     Channel1     Value1
OppA     Channel2     Value2
OppA     Channel3     Value3

表源

OppA     Channel1     Value4
OppA     Channel2     Value5
OppA     Channel4     Value6

预期结果:

OppA     Channel1     Value1     Value4
OppA     Channel2     Value2     Value5
OppA     Channel3     Value3     null
OppA     Channel4     null       Value6

我想我会通过加入得到我的结果

Select OppName, Channel, OppValue, SourceValue
from Opp 
full join Source on Opp.OppName = Source.SourceName and OppChannel = SourceChannel

当那没用的时候我试过了

Select OppName, Channel, OppValue, SourceValue
from Opp 
full outer join Source on OppChannel = SourceChannel
where Opp.OppName=OppA and Source.OppName=OppA

但不管怎样,我只得到2行返回(没有空值的两行)
请帮帮我((提前谢谢)

ogsagwnx

ogsagwnx1#

我想你只需要处理一下 NULL 价值观:

select coalesce(o.OppName, s.SourceName) as name,
       coalesce(o.OppChannel, s.SourceChannel) as Channel,
       o.OppValue, s.SourceValue
from Opp o full join
     Source s
     on o.OppName = s.SourceName and o.OppChannel = s.SourceChannel

相关问题