Solved

I need assistance using UNION in my SQL Query

Posted on 2013-11-18
4
3,328 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
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 65

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 69

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
t-sql Joining Issue 10 42
T-SQL: Do I need CLUSTERED here? 13 45
SQL View nearest date 5 37
SQL2016 to ORACLE11G linked-server 6 16
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

831 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