Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

I need assistance using UNION in my SQL Query

Posted on 2013-11-18
4
Medium Priority
?
3,400 Views
Last Modified: 2013-12-11
Hi Experts,
I am writing a query using UNION to join two tables in SQL Server 2012.  One table has 37,550 records, and the other table has 940,400 records, for a combined total of 977,950.  When I run my query using UNION, the result set only returns 885,067.  Why am I getting less records?

my query syntax:
SELECT [Column 0] AS SSN, [Column 1] AS fee,
[Column 2] AS DATE, 
[Column 4] AS OrderNo
FROM TABLE1
UNION
SELECT [Column 3] AS SSN, [Column 4] AS fee,
[Column 0] AS DATE, 
[Column 2] AS OrderNo
FROM TABLE2
ORDER BY OrderNo

Open in new window


Thanks in advance for your help,
mrotor..
0
Comment
Question by:mainrotor
[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
4 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 39658324
You need to use UNION ALL. UNION on its own omit the duplicate entries. So, the correct query will be:
SELECT [Column 0] AS SSN, [Column 1] AS fee,
[Column 2] AS DATE, 
[Column 4] AS OrderNo
FROM TABLE1
UNION ALL
SELECT [Column 3] AS SSN, [Column 4] AS fee,
[Column 0] AS DATE, 
[Column 2] AS OrderNo
FROM TABLE2
ORDER BY OrderNo

Open in new window

0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39658337
UNION ALL should be used when you're absolutely certain there will be no duplicates, and is just a set A + set B as a single return set.
SELECT name FROM whorehouses UNION ALL SELECT name FROM burgerjoints

Open in new window

UNION eliminates duplicates (as chaau stated above), and there is an extra cost to that as the query processor has to match up and remove rows from the final set, therefore should be avoided if possible.
SELECT name from customers UNION SELECT name FROM vendors

Open in new window

0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 39660050
>> UNION ALL should be used when you're absolutely certain there will be no duplicates <<

Not true.  There's no problem at all with returning duplicate rows using UNION ALL.  If you don't care about and/or want duplicates in the result set, use UNION ALL:

SELECT 'A' AS col1 UNION ALL SELECT 'A' UNION ALL SELECT 'A' UNION ALL SELECT 'A' UNION ALL SELECT 'A' --...


Otoh, UNION should be used only when you explicitly want/need to prevent duplicate rows from being returned by the query, because of the extra overhead to SQL of removing any potential duplicate rows.
0
 
LVL 32

Expert Comment

by:awking00
ID: 39660109
Since the difference between the total rows and the result set is less than all of the rows in the other table, there are obviously duplicate values for columns 0, 1, 2, and 4 in the first table by itself as well.
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

688 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