Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

sql server - Joining multiple tables returns NULL value

I have 3 different outcomes of CTE that I need to LEFT JOIN each other:

Main Table @Policies contains all VehiclePolicyLimitsID values:

enter image description here

@LiabilityPremium:

enter image description here

@HiredPremium:

enter image description here

As an example I mimic the results of CTE's into 3 table variables:

declare @Policies table (VehiclePolicyLimitsID int)
insert into @Policies values (2101891),
                             (2101892),
                             (2101893),
                             (2101894),
                             (2119235),
                             (2119236),
                             (2119237),
                             (2119238),
                             (2190860),
                             (2190861),
                             (2190862),
                             (2190863)
--select * from @Policies


declare @LiabilityPremium  table (Quoteid int, ClassCode int, VehiclePolicyLimitsID int, LiabilityPremium money)
insert into @LiabilityPremium values (728436,3199,2101892,1723),
                                     (728436, 23199,2101893,1855),
                                     (728436,68199,2101894,133),
                                     (741626,3199,2119236,0),
                                     (741626,23199,2119237,0),
                                     (741626,68199,2119238,0),
                                     (774168,3199,2190861,0),
                                     (774168,23199,2190862,0),
                                     (774168,68199,2190863,0)
--select * from @LiabilityPremium

declare @HiredPremium  table (Quoteid int, ClassCode int, VehiclePolicyLimitsID int, LiabilityPremium money)
                    insert into @HiredPremium values ( 728436,  NULL,   2101891,    25),
                                                     (741626,   NULL,   2119235,    0),
                                                     (774168,   NULL,   2190860,    0)

--select * from @HiredPremium
    select 
            COALESCE(l.Quoteid,h.QuoteID,'') as QuoteID,
            COALESCE(l.ClassCode,h.ClassCode,'') as ClassCode,
            COALESCE(l.VehiclePolicyLimitsID,h.VehiclePolicyLimitsID,'') as VehiclePolicyLimitsID,
            l.LiabilityPremium + h.LiabilityPremium as LiabilityPremium
    from @Policies p
    left join @LiabilityPremium l ON l.VehiclePolicyLimitsID = p.VehiclePolicyLimitsID
    left join @HiredPremium h ON h.VehiclePolicyLimitsID = p.VehiclePolicyLimitsID

But for some reason the outcome of LiabilityPremium is all NULL's:

enter image description here

I would expect the result looks like this with total LiabilityPremium = $3,736

enter image description here

Is any way to join somehow to receive desirable result?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That is because null on either side of the addition operator will yield a result of null. You can use ISNULL(LiabilityPremium, 0) Example:

ISNULL(l.LiabilityPremium,0) + ISNULL(h.LiabilityPremium,0) as LiabilityPremium

or you can use COALESCE instead of ISNULL.

COALESCE(l.LiabilityPremium,0) + COALESCE(h.LiabilityPremium,0) as LiabilityPremium

Edit

I am not sure if this is coincidence with this small data set or expected but if it is always expected that either @LiabilityPremium.LiabilityPremium or @HiredPremium.LiabilityPremium will always be null then there is no need to perform addition. Instead use COALESCE directly on those 2 columns.

COALESCE(l.LiabilityPremium, h.LiabilityPremium) as LiabilityPremium

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...