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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
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 …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

777 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