[Webinar] Streamline your web hosting managementRegister Today


How to use group by and union on a query?

Posted on 2009-05-04
Medium Priority
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 150 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

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…
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…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.

590 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