2014-11-24 3 views
0

그래서 나는 다음과 같은 논리가 :변환 실패

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

문제 InventoryType 중 하나 RO.Inventory_Type0 또는 1을 기반으로합니다 여기

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

인 경우 문 이 T1에서 모두 쿼리

case statement입니다. 그래서 여기에 InventoryType의 열 데이터 유형이 될 것입니다 T2에서 INT

이 경우 문

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

하지만 여기 InventoryTypeWIRELESS 또는 WIRED이 될 것입니다. 당신이 그 오류가되도록 varchar 값을 들고 int에 t2.InventoryType 변환하려고 그렇게 합류하면서 마지막으로 당신이

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

처럼 T1 및 T2 테이블을 조인하는 Varchar

여기 InventoryType의 datattype이 될 것입니다 .

+0

Gotcha! 답장을 보내 주셔서 감사합니다. 감사!! – user3038694

+0

@ user3038694 - 다행이 당신을 도왔습니다. –

관련 문제