【发布时间】:2020-05-30 19:19:24
【问题描述】:
我正在使用 SQL Server Management Studio 来构建一个报告问题的查询。
单独使用这些查询可以正常工作,但是一旦通过使用联合添加它们,我就会收到以下错误消息:
消息 245,第 16 级,状态 1,第 1 行
将 varchar 值“OFC”转换为数据类型 smallint 时转换失败
我尝试在w.wpage 和p.ppage 列上使用Cast 和Convert。我尝试在两个子查询的 case 语句中直接使用它们,这些子查询的结果各不相同,但始终是:
转换时转换失败...
错误。
任何帮助或建议将不胜感激。
SELECT
a.Article,
a.ARTICLENAME,
a.Offer,
a.EBD,
q.Excl,
a.OfferYear,
b.SeasonID,
CASE
WHEN CONCAT(catid,packnum) <> CONCAT(a.offer, a.article)
THEN 'Add'
WHEN a.EBD = 'B' AND q.Excl = 'BE'
THEN 'True'
ELSE 'Exclusive'
END AS Action,
CASE
WHEN CONCAT(a.offer, a.article) = CONCAT(d.ToCat, d.article)
THEN 'Y'
ELSE 'N'
END AS [On CN]
FROM
Lago_CID_Master a
INNER JOIN
CatalogInfo b ON (a.Offer = b.Catalog) AND (a.OfferYear = b.MailYear)
LEFT JOIN
ChangeNoticeCopyFile d ON (a.ARTICLE = d.article)
AND (a.Offer = d.ToCat) AND (a.OfferYear = d.ToYear)
LEFT JOIN
(SELECT
c.Packnum, c.CatID, c.Excl
FROM
PIC704Current c
INNER JOIN
cataloginfo d ON (c.CatID = d.catalog) AND (c.Year = d.MailYear)
WHERE
d.MailYear = '2020'
AND d.SeasonID = 'F20'
AND d.MediaId IN ('CAT Catalog', 'SCAT Sale Catalog', 'SSTF Sale Statement Stuff', 'STUF Statement Stuffer', 'PKG Package Insert', 'SPKG Sale Pkg Insert')
AND d.Brand = 'Seventh Avenue') q ON CONCAT(q.catid,q.packnum) = CONCAT(a.offer, a.article)
WHERE
q.CatID IS NULL
AND (CASE WHEN Retail < '1' THEN 'Yes' ELSE 'No' END) = 'No'
AND b.MailYear = '2020'
AND b.SeasonID = 'F20'
AND b.MediaId = 'Cat Catalog'
AND b.Brand = 'Seventh Avenue'
--Pages
UNION
SELECT DISTINCT
a.PackNum, a.Description, a.CatID,
c.SeasonID, a.Page as MFPage, w.WPage, p.Ppage,
CASE
WHEN a.page <> w.wpage
THEN 'Update Page'
ELSE ''
END AS action,
CASE
WHEN CONCAT(a.CatID, a.PackNum) = CONCAT(d.offer, d.article)
THEN 'Y'
ELSE 'N'
END AS [On CN]
FROM
pic704current a
INNER JOIN
Lago_CID_Master b ON (a.CatID = b.Offer)
AND (a.year = b.OfferYear) AND (a.PackNum = b.ARTICLE)
JOIN
CatalogInfo c ON (a.catid = c.catalog) AND (a.year = c.mailyear)
FULL OUTER JOIN
ChangeNoticeChanges d ON (b.ARTICLE = d.article)
AND (a.CatID = d.offer) AND (a.Year = d.offeryear)
LEFT JOIN
--Working Page
(SELECT
e.packnum, e.catid,
CASE
WHEN CAST(f.wpfrom AS int) >= 7000 THEN 'EOC'
WHEN CAST(f.wpfrom AS int) BETWEEN 700 AND 800
AND f.spreadname LIKE '%CHECK%' THEN 'CHK'
WHEN f.spreadname LIKE '%ORDER FORM%'
OR f.spreadname LIKE '%CTOB%' THEN 'COF'
WHEN f.spreadname LIKE '%OFC%' THEN 'OFC'
WHEN f.spreadname LIKE '%EXOBC%' THEN 'WR' +
CASE WHEN SUBSTRING(f.spreadname, CHARINDEX(e.catid, f.spreadname) + 2, 1) = ' '
OR SUBSTRING(f.spreadname, CHARINDEX(f.offer, f.spreadname) + 2, 1) = '' THEN '1'
ELSE SUBSTRING(f.spreadname, CHARINDEX(f.offer, f.spreadname) + 2, 1)
END
WHEN f.spreadname LIKE '%IBC%' THEN 'IB' +
CASE WHEN substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = ' ' or substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = '' then '1'
ELSE substring(spreadname,charindex(f.offer, f.spreadname)+2,1) End
WHEN Cast(f.wpfrom as int) = 2 then 'IF' + case when charindex(f.offer, f.spreadname) = 0 then '1' when substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = '' then '1'
ELSE substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) End
WHEN f.spreadname like '%OBC%' then 'OB' + case when substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = ' ' or substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) = '' then '1'
ELSE substring(f.spreadname,charindex(f.offer, f.spreadname)+2,1) End
ELSE right('00' + f.wpfrom,3) END AS WPage
FROM pic704current e INNER JOIN Lago_CID_Master f ON (e.CatID = f.Offer) and (e.year = f.OfferYear) and (e.PackNum = f.ARTICLE)
JOIN CatalogInfo g on (e.catid = g.catalog) and (e.year = g.mailyear)
WHERE
e.year = '2020'
and g.SeasonID = 'F20'
and g.brand = 'Seventh Avenue'
and g.MediaId in('CAT Catalog','SCAT Sale Catalog','SSTF Sale Statement Stuff','STUF Statement Stuffer','PKG Package Insert','SPKG Sale Pkg Insert')) w
ON CONCAT(a.catid,a.packnum) = CONCAT(w.CatID, w.PackNum)
LEFT JOIN
--Paginated Page
(SELECT I.packnum, I.catid
,CASE WHEN Cast(J.PPFROM as int) >=7000 then 'EOC'
WHEN Cast(J.PPFROM as int) between 700 and 800 and J.spreadname like '%CHECK%' then 'CHK'
WHEN J.spreadname like '%ORDER FORM%' or J.spreadname like '%CTOB%' then 'COF'
WHEN J.spreadname like '%OFC%' then 'OFC'
WHEN J.spreadname like '%EXOBC%' then 'WR' + case when substring(J.spreadname,charindex(I.catid, J.spreadname)+2,1) = ' ' or substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1'
ELSE substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) End
WHEN J.spreadname like '%IBC%' then 'IB' + case when substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = ' ' or substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1'
ELSE substring(spreadname,charindex(J.offer, J.spreadname)+2,1) End
WHEN Cast(J.PPFROM as int) = 2 then 'IF' + case when charindex(J.offer, J.spreadname) = 0 then '1' when substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1'
ELSE substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) End
WHEN J.spreadname like '%OBC%' then 'OB' + case when substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = ' ' or substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) = '' then '1'
ELSE substring(J.spreadname,charindex(J.offer, J.spreadname)+2,1) End
ELSE right('00' + J.PPFROM,3) END AS PPage
FROM pic704current I INNER JOIN Lago_CID_Master J ON (I.CatID = J.Offer) and (I.year = J.OfferYear) and (I.PackNum = J.ARTICLE)
JOIN CatalogInfo K on (I.catid = K.catalog) and (I.year = K.mailyear)
WHERE
I.year = '2020'
and K.SeasonID = 'F20'
and K.brand = 'Seventh Avenue'
and K.MediaId in('CAT Catalog','SCAT Sale Catalog','SSTF Sale Statement Stuff','STUF Statement Stuffer','PKG Package Insert','SPKG Sale Pkg Insert'))P
ON CONCAT(a.catid,a.packnum) = CONCAT(p.CatID, p.PackNum)
WHERE
(CASE WHEN wpage <> a.page and ppage = '000' then 'true' else 'false' end) = 'true'
and
(CASE WHEN ppage > '0' and ppage <> a.page then 'true' else 'false' end) = 'true'
and a.year = '2020'
and c.SeasonID = 'F20'
and c.brand = 'Seventh Avenue'
and c.MediaId in('CAT Catalog','SCAT Sale Catalog','SSTF Sale Statement Stuff','STUF Statement Stuffer','PKG Package Insert','SPKG Sale Pkg Insert')
ORDER BY Action
【问题讨论】:
-
你想对 Union 做什么?如果 Union 要正常工作,则必须匹配 2 个数据集中的每一列。前任。如果第一个查询的列是 (int, varchar, date) 第二个查询也应该是相同的。由于您的查询太长且缺少其他信息,因此很难找到问题的原因。
-
为什么在使用
UNION时使用DISTINCT?另外,我真的建议使用一些 good 别名。 Bad habits to kick : using table aliases like (a, b, c) or (t1, t2, t3) -
@SrinikaPinnaduwage 这就是问题所在!一旦我排列了我的列以匹配我就能够毫无问题地运行我的查询。感谢您的建议!
-
@Larnu 谢谢,我显然还在学习和自学。我很感激这个资源,一定会去看看!
标签: sql sql-server