Solved

How to use group by and union on a query?

Posted on 2009-05-04
3
318 Views
Last Modified: 2012-05-06
Hi

What's wrong with the following 2 SQL? Both have error:

All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.

select type from account group by type - It works


select type from account group by type union select -2 as type, '-- Please Select --' as type from account 
 
select type from account group by type union select -2 as type, '-- Please Select --' as type from account  group by type

Open in new window

0
Comment
Question by:techques
  • 2
3 Comments
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24301533
union should have same number of column in both the query,

you have one column "TYPE" in first SELECt and two TYPE in second.
0
 
LVL 31

Accepted Solution

by:
RiteshShah earned 50 total points
ID: 24301538
have a look
-- will not work
select 1 as type
union
select -2 as type, 3 as type
 
 
-- will work
select 1 as type
union
select -2 as type

Open in new window

0
 

Author Closing Comment

by:techques
ID: 31577875
excellent!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question