A união de várias tabelas retorna um valor NULL
Eu tenho três resultados diferentes da CTE que preciso deixar para me juntar:
Tabela principal@Policies
contém tudoVehiclePolicyLimitsID
valores:
@LiabilityPremium
:
@HiredPremium
:
Como exemplo, imitamos os resultados de CTEs em 3 variáveis de tabela:
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
Mas, por alguma razão, o resultado do LiabilityPremium é todo NULL:
Eu esperaria que o resultado se parecesse com o LiabilityPremium total = US $ 3.736
Existe alguma maneira de participar de alguma forma para obter resultados desejáveis?