问题描述
我的要求是连接两个具有名为Site,Date,BusLine的公共列的表。我使用完全外连接来加入后转储该数据事实表。问题是事实表的列数为数字的数据类型,源表
将所有列都作为varchar.I不能将源表中的那些列的类型更改为数字,因为应用程序填充表错误并拒绝行,这不应该发生。所以我决定在我的结尾转换并将它们全部加载到事实表中。
My requirement is to join two tables that have common columns named Site,Date,BusLine.I am using full outer join to dump that data after joining into a fact table.The issue is the fact table has data types of number of columns as numeric and the source tables have all of them as varchar.I cant change the type of those columns in source table to numeric because the app that populates the table errors out and rejects the rows,which shouldnt happen.So i decided to convert at my end and load them all into a fact table.
我已经成功加载了通过以下查询没有连接将char类型转换为数字。当我与另一个具有相似字符类型的表进行连接时,我在连接查询中出现错误,说"无法将varchar转换为数字".How
我得到了加入它们并将varchar加载到numeric中。我在这里提供了两个源表的示例以及我正在处理的查询以使其工作。
I have been successful in loading the char types into numeric by following query without join.The moment i do a join with another table which has similar character types,i get error in my join query saying "cant convert varchar to numeric".How do i get to join them and load varchar into numeric.I have provided sample of two source tables here and the query i am working on to get this to work.
TABLE-1
TABLE-2
我对新表的查询表3包含4列:站点varchar,BusLine varchar,#Item numeric,#Inventory numeric
My query into new table Table-3 that contains 4 columns:Site varchar,BusLine varchar,#Item numeric,#Inventory numeric
插入表-3
选择coalesce(a.Site,b.Site)网站
    ,合并(a.BusLine,b.busline)BusLine
    ,合并时的情况(a.busline,b.busline)='CC'和([#Item]!= null或[#Item]!='')
跨度>       然后施放(替换([#Item],',','。')作为数字(30,3))
跨度>       当[#Item] =''然后为null
else replace([#Item],',','')
end #Item
,合并时的情况(a.busline,b.busline)='CC'和([#Inventory]!= null或[#Inventory]!='')
跨度>       然后施放(替换([#库存],',','。')作为数字(30,3))
跨度>       当[#Inventory] =''然后为null时,
其他替换([#S inventory],',','')
end #Inventory
 从表-1中a.site = b.site上的完全外连接表-2b和a.BusLine = b.BusLine
select coalesce(a.Site,b.Site) site
,coalesce(a.BusLine,b.busline) BusLine
,case when coalesce(a.busline,b.busline)='CC' and ([#Item]!=null or [#Item]!='')
then cast(replace([#Item],',','.') as numeric(30,3))
when [#Item]='' then null
else replace([#Item],',','')
end #Item
,case when coalesce(a.busline,b.busline)='CC' and ([#Inventory]!=null or [#Inventory]!='')
then cast(replace([#Inventory],',','.') as numeric(30,3))
when [#Inventory]='' then null
else replace([#Inventory],',','')
end #Inventory
from Table-1 a full outer join Table-2 b on a.site=b.site and a.BusLine=b.BusLine
我正在做以上替换是因为Table1和Table 2中的字符串列包含BusLine = CC的逗号等字符,因此我将它们替换为十进制,并将所有其他逗号替换为''替换其他BusLines。
I am doing the above replace because the string columns in Table1 and Table 2 contain characters like comma for BusLine=CC,hence i replace them with decimal and replace all other commas with '' for other BusLines.
没有连接的查询确实将数据分别插入表#3中的列#Item和#Inventory。我很惊讶为什么它不会让我在加入时填充这两列并抱怨转换。请帮助
The query without join does insert data into Table-3 for columns #Item and #Inventory individually.I am surprised why it doesnt let me populate those two columns together when joined and complains about conversion.Pls help
奇怪:
从表1中选择*到表-3中a.site上的完整外连接表-2 b = b.site和a.BusLine = b.BusLine将数据加载到表-3中
select * into Table-3 from Table-1 a full outer join Table-2 b on a.site=b.site and a.BusLine=b.BusLine loads data into table-3
但是插入表-3选择... 不起作用
But insert into Table-3 select ... doesnt work
推荐答案
样本在哪里?
我错过了一个链接吗?
我只看图像而没有样本表
where is the sample?
Am I missing a link?
I only see images and no sample tables
请发布查询以创建表格并插入样本数据
Please post queries to create the tables and insert the sample data
这篇关于完全外连接dosnt使用错误,无法将varchar转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!