Solved

Is this a correct use of UNION?

Posted on 2013-05-28
11
307 Views
Last Modified: 2013-05-28
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
Comment
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
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 49

Expert Comment

by:PortletPaul
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

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

is this what you need?
0
 
LVL 16

Expert Comment

by:Surendra Nath
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

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Expert Comment

by:PortletPaul
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

by:ValentinoV
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

by:rhservan
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

by:
PortletPaul earned 500 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

by:PortletPaul
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

by:ValentinoV
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

by:rhservan
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

by:rhservan
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

689 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