2017-07-07 135 views
0

我一直在盯着這個查詢20分鐘,我看不出爲什麼我在標題中出現錯誤。你能理解爲什麼會發生這種情況嗎?謝謝!將varchar值「D」轉換爲數據類型時轉換失敗int

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; 

DECLARE @StartDate DATETIME = '2016-06-05 00:00:00.000'; 
DECLARE @EndDate DATETIME = '2016-08-06 23:59:59.000'; 
select count(tptC.MovConID) as [Count of Collections], sum(cgtC.[Chargeable weight]) as [Total Col Chg Wt], sum(tptrevenueC.amount) as [Rev Col], 
count(tptD.MovConID) as [Count of Deliveries], sum(cgtD.[Chargeable weight]) as [Total Del Chg Wt], sum(tptrevenueD.amount) as [Rev Del] 
from tptVehicle tptv 
inner join tptHeader tpth on tpth.VehicleID = tptv.VehicleID 
left join tptDetails tptC on tptC.RunID = tpth.RunID and tptC.RunType = 'C' 
left join tptDetails tptD on tptD.RunID = tpth.RunID and tptD.RunID = 'D' 
left join tptVehicleRevenueDaily tptrevenueC on tptrevenueC.Reference = tptC.MovConID 
left join tptVehicleRevenueDaily tptrevenueD on tptrevenueD.Reference = tptD.MovConID 
left join cgtConsignment cgtC on cgtC.[Consignment Reference] = tptrevenueC.Reference 
left join cgtConsignment cgtD on cgtD.[Consignment Reference] = tptrevenueD.Reference 
where tpth.JourneyDate >= @StartDate and tpth.JourneyDate <= @EndDate and tpth.RunReference like 'belf%' 
+1

什麼'tptD.RunID'的數據類型? – SchmitzIT

+0

@SchmitzIT - 你的問題讓我意識到我的錯誤,現在我已經糾正它,所以它工作正常!謝謝:) –

+0

不用擔心,我很樂意幫助:) – SchmitzIT

回答

1

大概是因爲tpt.RunID是int,而你想在你的left join S的一個比較'D'

嘗試修改此:

left join tptDetails tptD on tptD.RunID = tpth.RunID and tptD.RunID = 'D' 

這樣:

left join tptDetails tptD on tptD.RunID = tpth.RunID and tptD.RunType = 'D' 
相關問題