We help IT Professionals succeed at work.

sql union

201 Views
Last Modified: 2015-10-14
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
Comment
Watch Question

Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

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

no one answer
Vitor MontalvãoIT Engineer
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
How do you mean no one answered?
I can see Éric's comment.
Vikas GargAssociate Principal Engineer
CERTIFIED EXPERT
Top Expert 2014

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.