Solved

I need assistance using UNION in my SQL Query

Posted on 2013-11-18
4
3,293 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
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
Comment Utility
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 65

Expert Comment

by:Jim Horn
Comment Utility
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 69

Expert Comment

by:ScottPletcher
Comment Utility
>> 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 31

Expert Comment

by:awking00
Comment Utility
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

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

771 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

11 Experts available now in Live!

Get 1:1 Help Now