Solved

How to use group by and union on a query?

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

21 Experts available now in Live!

Get 1:1 Help Now