Solved

How to use group by and union on a query?

Posted on 2009-05-04
3
319 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
In this article I will describe the Backup & Restore 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.
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…

713 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