How to use group by and union on a query?

Posted on 2009-05-04
Last Modified: 2012-05-06

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

Question by:techques
  • 2
LVL 31

Expert Comment

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.
LVL 31

Accepted Solution

RiteshShah earned 50 total points
ID: 24301538
have a look
-- will not work

select 1 as type


select -2 as type, 3 as type

-- will work

select 1 as type


select -2 as type

Open in new window


Author Closing Comment

ID: 31577875

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

932 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now