所以我有以下逻辑:
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值时转换失败' WIRED'到数据类型int。
我到处搜索并尝试将其投射到varchar,但我觉得我缺乏经验并没有帮助。如果有人能够指出我为什么会收到这个错误,那么我就可以学习如何解决这个错误!
ps:抱歉我的英文。
答案 0 :(得分:1)
问题出在两个查询的case statement
在T1
中有案例陈述
CASE RO.Inventory_Type WHEN 'WIRELESS' THEN 1 ELSE 0 END AS InventoryType
此处InventoryType将基于0
为1
或RO.Inventory_Type
。所以这里的InventoryType列数据类型将是INT
在T2中有案例陈述
CASE WHEN InventoryType = 1 THEN 'WIRELESS' ELSE 'WIRED' END AS InventoryType
但此处InventoryType
可以是WIRELESS
或WIRED
。 InventoryType数据类型将为Varchar
最后,您将加入T1和T2表格,如
ON t1.Client_Number = t2.client_number
and t1.BillingMonth = t2.BillingMonth
and t1.InventoryType = t2.InventoryType -- this where the problem is
因此,在加入时,尝试将t2.InventoryType转换为int
,其中包含varchar
值,以便您收到该错误。