Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to use group by and union on a query?

Posted on 2009-05-04
3
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 150 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

There are some very powerful Dynamic 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 di…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

604 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