troubleshooting Question

sql union

Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America asked on
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008SSRS
4 Comments1 Solution225 ViewsLast Modified:
I have below sql. and if I take out UNION. All works fine.
If I only two 2nd and 3 rd (combined together). It works fine as well.

But when I combined all three. they are not working as shown error like (Error Converting Data Type varchar to numeric
why? and how to fix it?


SELECT '0' AS LEV, C1105 AS DealerName, C1100 AS CustomerName, C1025 AS Probability, SUBSTRING(MJ007,1,30) AS StatusName, C1004 AS QuoteNo,C1005 AS Seq, CONVERT(char,C1022) AS ExpClose, C2007 AS ModelNo, C2010 AS Qty, C2014 AS UnitPrice, C2015 AS ExtnPrice, C1031 AS SubTotal FROM JC1 INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 LEFT OUTER JOIN JN6 ON C1101 = N6004 LEFT OUTER JOIN JN8 ON C1018 = N8004 INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02'
 
UNION
SELECT '1' AS LEV, C1105 AS DealerName, '99&' AS CustomerName, '99&' AS Probability, '99&' AS StatusName, '99&' AS QuoteNo,'99&' AS Seq, '99&' AS ExpClose, '99&' AS ModelNo, Sum(C2010) AS Qty, Sum(C2014) AS UnitPrice, Sum(C2015) AS ExtnPrice, Sum(C1031) AS SubTotal FROM JC1 INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 LEFT OUTER JOIN JN6 ON C1101 = N6004 LEFT OUTER JOIN JN8 ON C1018 = N8004 INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02' GROUP BY C1105

UNION
SELECT '2' AS LEV, '99&' AS DealerName, '99&' AS CustomerName, '99&' AS Probability, '99&' AS StatusName, '99&' AS QuoteNo,'99&' AS Seq, '99&' AS ExpClose, '99&' AS ModelNo, Sum(C2010) AS Qty, Sum(C2014) AS UnitPrice, Sum(C2015) AS ExtnPrice, Sum(C1031) AS SubTotal FROM JC1 INNER JOIN JMJ ON C1013 = MJ006 AND MJ004 = 'JS' AND MJ005 = 210 LEFT OUTER JOIN JN6 ON C1101 = N6004 LEFT OUTER JOIN JN8 ON C1018 = N8004 INNER JOIN JC2 ON C1004 = C2004 AND C1005 = C2005 WHERE C1055 = '31' AND C1024 >= 20150901 AND C1024 <= 20150930 and C1013 = '02' ORDER BY DealerName,LEV
ASKER CERTIFIED SOLUTION
Éric Moreau
Senior .Net Consultant
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros