Avatar of ITsolutionWizard
ITsolutionWizard
Flag for United States of America asked on

sql union

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
SSRSMicrosoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Vikas Garg

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Éric Moreau

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ITsolutionWizard

ASKER
I've requested that this question be deleted for the following reason:

no one answer
Vitor Montalvão

How do you mean no one answered?
I can see Éric's comment.
Vikas Garg

Hello,

You can try this

SELECT '0' AS LEV, CONVERT(VARCHAR(50),C1105) AS DealerName, CONVERT(VARCHAR(50),C1100) AS CustomerName, CONVERT(VARCHAR(50),C1025) AS Probability, SUBSTRING(CONVERT(VARCHAR(50),MJ007),1,30) AS StatusName, 
CONVERT(VARCHAR(50),C1004) AS QuoteNo,CONVERT(VARCHAR(50),C1005) AS Seq, CONVERT(char,C1022) AS ExpClose, CONVERT(VARCHAR(50),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 

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck