0
別のタブで選択された別のタブを選択して情報を表示するにもかかわらず、revenue.RevCとrevenue.RevDがNullとして表示される理由を理解したいと思います。ジョインの不適切な使用 - それを修正する方法
[rev col]列と[rev del]列にデータが格納されないようにするために、私が行っている間違いを見つけられますか?私はそれが左の結合を使うことによると信じています。私は正しいですか?
with CTE as
(
select count(tpt1.MovConID) as [Collections],
count(tpt2.MovConID) as [Count of Del],
sum (c1.[Chargeable Weight]) as [Total Col Chg Wt],
sum(c2.[Chargeable Weight]) as [Total Del Chg Wt],
revenueC.RevC as [Rev Col],
revenueD.RevD as [Rev Del]
from tptvehicle tptv
left JOIN tptHeader tpth ON tpth.VehicleID = tptV.VehicleID
left JOIN tptVehicleRevenueDaily tptrevenue on tptrevenue.VehicleReg = tptv.RegNo
left Join (select tpth.RunReference, tptV.VehicleID, sum(tptvehicle.Amount) as RevC from tptVehicleRevenueDaily tptvehicle
inner join tptVehicle tptv on tptv.RegNo = tptvehicle.VehicleReg
inner join tptHeader tptH on tptH.VehicleID = tptv.VehicleID
inner join tptDetails tpt on tpt.RunID = tptH.RunID where tpt.RunType = 'C'
group by tpth.RunReference, tptv.VehicleID) revenueC on revenueC.RunReference = Tpth.RunReference
Left Join (select tpth.RunReference, tptV.VehicleID, sum(tptvehicle.Amount) as RevD from tptVehicleRevenueDaily tptvehicle
inner join tptVehicle tptv on tptv.RegNo = tptvehicle.VehicleReg
Inner join tptHeader tptH on tptH.VehicleID = tptv.VehicleID
Inner join tptDetails tpt on tpt.RunID = tptH.RunID where tpt.RunType = 'D'
group by tpth.RunReference, tptv.VehicleID) revenueD on revenueD.VehicleID = TptV.VehicleID
LEFT JOIN tptDetails tpt1 ON tpt1.RunID = tptH.RunID and tpt1.RunType = 'C'
LEFT JOIN cgtConsignment c1 on c1.[Consignment Reference] = tpt1.MovConID
LEFT JOIN tptDetails tpt2 on tpt2.RunID = tptH.RunID and tpt2.RunType = 'D'
LEFT Join cgtConsignment c2 on c2.[Consignment Reference] = tpt2.MovConID
where tpth.RunReference like 'bham-%' and
tptH.JourneyDate >= @StartDate and
tpth.JourneyDate <= @EndDate
Group by revenueC.RevC, revenueD.RevD
)
Select cte.collections, cte.[count of del], cte.[Total Col Chg Wt], cte.[Total Del Chg Wt], cte.[Rev Col], cte.[Rev Del]
from CTE
データの問題。 'revenueC'と' revenueD'は(左の)外部結合を介して結合されます。つまり、結合条件が一致しないと、代わりにNULL行を返します。 – RBarryYoung
tpthがLEFT JOINされ、WHERE句がtpthに対してINNER JOINになります。 – Wendy
CTEの内容(定義)をすべて実行しようとするとどうなりますか? – Eli