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

x
Solved

# Is this a correct use of UNION?

Posted on 2013-05-28
Medium Priority
310 Views
When using a UNION to eliminate duplicates, will it work when using count as in columnB below?

ColumnA, count(distinct columnB)
From TableA

UNION

ColumnA, count(distinct columnB)
From TableA
0
Question by:rhservan
[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
• 5
• 3
• 2
• +1

LVL 49

Expert Comment

ID: 39200786
it would be pointless

both the upper and lower queries would produce the same results, so the union will disgard one row

you might as well only do the upper query.

I suspect there's more here
0

LVL 49

Expert Comment

ID: 39200790
select
ColumnA, count(distinct columnB)
From TableA
group by ColumnA

is this what you need?
0

LVL 16

Expert Comment

ID: 39200793
yes it will, but you can re-write as below

``````ColumnA, count(distinct columnB)
From TableA group by ColumnA

UNION

ColumnA, count(distinct columnB)
From TableA group by ColumnA
``````
0

LVL 49

Expert Comment

ID: 39200815
but it remains pointless... I'm not following (or my eyes are deceiving me)

let's say the upper query produces this

ABCD 1
DEFG 3

and the lower produces exactly the same, together when "stacked" and sorted would be:

ABCD 1
ABCD 1
DEFG 3
DEFG 3

the UNION then looks for repeats, and the end result is:

ABCD 1
DEFG 3

i.e. equivalent to the upper query

Note, UNION considers the "whole row"
0

LVL 37

Expert Comment

ID: 39200981
rhservan, this is a weird question, it looks like you're not mentioning a crucial part of the story. As Paul already pointed out, query A and query 2 are exactly the same and thus produce the same result, which is then "undone" by the UNION.

Or are you actually trying to duplicate your data? In that case you can use UNION ALL.
0

Author Comment

ID: 39201192
I'm not sure what I am missing:

If ColumnB in both queries are based on a count, not a list, does the UNION still recognize
duplication of the column being counted?
0

LVL 49

Accepted Solution

PortletPaul earned 2000 total points
ID: 39201237
UNION works across "the whole row", it does not matter if a column is the result of an aggregation - the values of such a column in a row are still considered

the WHOLE row must differ (in some way) to survive the union operation

What UNION will NOT understand is "duplication of the column being counted" because it will  ONLY assess the RESULT of the count.

if you need to remove "duplication of the column being counted" you must do that before counting them.
0

LVL 49

Expert Comment

ID: 39201245
maybe if I add this it will help...

making rows unique by UNION occurs AFTER each of the subqueries are performed, union therefore only considers the results (not how you got to the results)
0

LVL 37

Expert Comment

ID: 39201274
Perhaps we can help you better if you explain a bit more what you're actually trying to achieve with that query...  Show us a couple of data examples.
0

Author Comment

ID: 39201534
PP this is exactly what I was looking for:

"What UNION will NOT understand is "duplication of the column being counted" because it will  ONLY assess the RESULT of the count.

if you need to remove "duplication of the column being counted" you must do that before counting them."

How can I accomplish this?
0

Author Comment

ID: 39201588
You mean, logically, something like this would work:

ColumnA, ColumnB , count(distinct columnB)
From TableA

Group By ColumnA, ColumnB

UNION

ColumnA, ColumnB , count(distinct columnB)
From TableA

Group By ColumnA, ColumnB
0

## Featured Post

Question has a verified solution.

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

In this article I will describe the Copy Database Wizard 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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper cornâ€¦
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. â€¦
###### Suggested Courses
Course of the Month8 days, 15 hours left to enroll