我有来自多个不同来源的数据,试图将它们组合成一个整洁的表,然后将其用于将来的所有查询。决赛桌将列出订单。
我已经解决了两个查询,而我要做的就是UNION它们。单独运行查询不会返回任何问题,但是当我尝试将它们合并到一个表中时,会得到
将数据类型varchar转换为数字时出错
我的第二张表几乎是所有varchar
列。我假设此错误源于我的第一个表具有数字列,并且编译器正在尝试将第二个表的列的数据类型匹配到第一个表。
问题是,我尝试将所有数字列手动转换为数字,但仍然始终会出现相同的错误。有人有建议吗?我应该尝试翻转表格的顺序吗?还有更优雅的解决方案吗?
以下是查询:
SELECT *
INTO SpArchivedOrders
FROM
(SELECT
CONVERT(Date,I.Date_Created) Date_Created,
I.Company_Code,
I.Division_Code,
CAST(I.Invoice_Number AS NUMERIC) Invoice_Number, -- numeric
Sh.CUST_PO,
I.Total_Quantity, -- numeric
ID.Total, -- money?
SH.Ship_City City,
CASE
WHEN SH.Ship_Cntry <> 'US'
THEN 'INT'
ELSE SH.Ship_prov
END State,
SH.Ship_Zip Zip,
SH.Ship_Cntry Country,
MONTH(I.Date_Created) Month,
S.CustomerEmail --varchar
FROM
[JMNYC-AMTDB].[AMTPLUS].[dbo].Invoices I (nolock)
LEFT JOIN
(SELECT
ID.Company_Code, ID.Division_Code,
ID.Invoice_Number,
SUM (ID.Price * ID.Quantity) Total
FROM
[JMNYC-AMTDB].[AMTPLUS].[dbo].Invoices_Detail ID (nolock)
GROUP BY
ID.Company_Code, ID.Division_Code, ID.Invoice_Number) ID ON I.Company_Code = ID.Company_Code
AND I.Division_Code = ID.Division_Code
AND I.Invoice_Number = ID.Invoice_Number
LEFT JOIN
[JMDNJ-ACCELSQL].[A1WAREHOUSE].[dbo].SHIPHIST SH (nolock) ON I.Pickticket_Number = SH.Packslip
LEFT JOIN
[JMDNJ-ACCELSQL].[A1WAREHOUSE].[dbo].[SpMagentoCustomerEmailData] S ON SH.CUST_PO = S.InvoiceNumber
WHERE
I.Company_Code = '09'
AND I.Division_Code = '001'
AND I.Customer_Number = 'ECOM2X'
AND ISNUMERIC(SH.CUST_PO) <> 0
AND CustomerEmail IS NOT NULL
UNION
SELECT
CONVERT(DATE, DATEADD(day, CAST(SS.OrderDate AS INT), '1899-12-30')) Date_Created,
'09' Company_Code, '001' Division_Code,
CAST('00000' AS Numeric) Invoice_Number,
SS.OrderNumber CUST_PO,
CAST(SS.TotalQuantity AS NUMERIC) Total_Quantity,
CAST(SS.OrderAmount AS NUMERIC) Total,
SS.ShippingCity City,
CASE
WHEN SS.CountryCode <> 'US'
THEN 'INT'
ELSE SS.StateCode
END State,
SS.ShippingZip Zip, SS.CountryCode Country,
MONTH(CONVERT(DATE, DATEADD(DAY, CAST(SS.OrderDate AS INT), '1899-12-30'))) Month,
SS.Email CustomerEmail
FROM
SpShopifyDataNew SS (nolock)) T
ORDER BY
Date_Created DESC