2014-11-24 111 views
0

所以,我有以下邏輯:轉換轉換爲varchar值「有線」爲int數據類型時失敗

set nocount on 

select t1.*, ISNULL(t2.CountNewGLLinkIDs,0) AS CountNewGLLinkIDs 

from 

(select [Client_Number],[ClientName],[RemitType],[ClientServiceRep],[Backup_ClientServiceRep],  [ClientAuditor],[WirelessAuditor] 
,[AccountManager],[ProvisioningRep],[BillingMonth], CASE RO.Inventory_Type WHEN 'WIRELESS' THEN 1  ELSE 0 END AS InventoryType, [NbrGLLinkIDs],[NbrInvoices],[AutoProcessCount] as [AutoProcessed] 
,CONVERT(numeric(18,4), CONVERT(decimal, [AutoProcessCount])/CASE WHEN ISNULL([NbrInvoices], 1) =  0 THEN 1 ELSE ISNULL([NbrInvoices], 1) END) as AutoProcessPercentage 
,[Spend],[EDI] 
,CONVERT(numeric(18,4), CONVERT(decimal, [EDI])/CASE WHEN ISNULL([NbrInvoices], 1) = 0 THEN 1  ELSE ISNULL([NbrInvoices], 1) END) as EDIPercentage 
,[Paper] 
,CONVERT(numeric(18,4), CONVERT(decimal, [PAPER])/CASE WHEN ISNULL([NbrInvoices], 1) = 0 THEN 1  ELSE ISNULL([NbrInvoices], 1) END) as PaperPercentage 
,[Import] 
,CONVERT(numeric(18,4) , CONVERT(decimal, [Import])/CASE WHEN ISNULL([NbrInvoices], 1) = 0 THEN 1  ELSE ISNULL([NbrInvoices], 1) END) as ImportPercentage 
,[TotalLateFees] 
,[TotalLateFees]/CASE WHEN ISNULL([Spend], 1) = 0 THEN 1 ELSE ISNULL([Spend], 1) END as  LateFeesPercentOfSpend 
,[NumberOfLateFees],[BalanceCarriedForward] 
,[BalanceCarriedForward]/CASE WHEN ISNULL([Spend], 1) = 0 THEN 1 ELSE ISNULL([Spend], 1) END as  BCFPercentOfSpend 
,[NumberOfBCFs],[ApprovedWithin5DaysOfDue],[ApprovedAfterDue],[ProcessedWithin5DaysOfDue], [ProcessedAfterDue],[AvgDaysToProcess] 
,[NewMasterAccounts] 
,[BANCount] AS [NewAccountBANCount] 
FROM [RollupReports].[dbo].[report_Rollup_KPI_Approval] RO with (nolock)) t1 


left outer join 

--get count of gllinkid during months required 
(select client_number, CASE WHEN InventoryType = 1 THEN 'WIRELESS' ELSE 'WIRED' END AS  InventoryType, 
cast(datepart(mm,DateCreated) as varchar(2)) + '/01/' + cast(datepart(yyyy,DateCreated) as  varchar(4)) as BillingMonth, 
count(gllinkid) as CountNewGLLinkIDs 
from glacct with (nolock) 
inner join 
    (select vendor,MAX(InventoryType) as InventoryType 
    FROM tbl_Ref_Vendors with(nolock) 
    group by vendor) as VendorData ON glacct.vendor = VendorData.Vendor 
where Client_Number in 
    (select distinct client_number 
    FROM [RollupReports].[dbo].[report_Rollup_KPI_Approval] with (nolock)) 
group by client_number, cast(datepart(mm,DateCreated) as varchar(2)) + '/01/' +  cast(datepart(yyyy,DateCreated) as varchar(4)), 
InventoryType) t2 

ON t1.Client_Number = t2.client_number and t1.BillingMonth = t2.BillingMonth and t1.InventoryType  = t2.InventoryType 
order by t1.ClientName, t1.InventoryType, t1.BillingMonth DESC 

其在這裏給了我這個錯誤:轉換VARCHAR值「有線」到數據時轉換失敗鍵入int。

我到處搜索並嘗試將其轉換爲varchar,但我覺得我缺乏經驗並沒有幫助。如果有人可以指出爲什麼我得到這個錯誤,所以我可以學習如何解決這將是偉大的!

ps:對不起,我的英語。

回答

1

問題是在這兩個查詢

case statementT1有case語句 這是

CASE RO.Inventory_Type WHEN 'WIRELESS' THEN 1  ELSE 0 END AS InventoryType 

這裏InventoryType將基於RO.Inventory_Type要麼01。所以這裏的InventoryType列數據類型將是INT

在T2有case語句

CASE WHEN InventoryType = 1 THEN 'WIRELESS' ELSE 'WIRED' END AS InventoryType 

但這裏InventoryType將是要麼WIRELESSWIRED。這裏InventoryType datattype將Varchar

最後你是加盟像

ON t1.Client_Number = t2.client_number 
and t1.BillingMonth = t2.BillingMonth 
and t1.InventoryType = t2.InventoryType -- this where the problem is 

T1和T2表,以便同時連接它正試圖在t2.InventoryType轉換爲int其持有varchar值,以便您獲得的錯誤。

+0

哎呀!非常感謝你的回答,它引導我解決它!謝謝!! – user3038694 2014-11-24 18:12:46

+0

@ user3038694 - 很高興幫助你 – 2014-11-24 18:16:59

相關問題