Link to home
Start Free TrialLog in
Avatar of rhservan
rhservanFlag for United States of America

asked on

Is this a correct use of UNION?

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
Avatar of PortletPaul
PortletPaul
Flag of Australia image

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
select
ColumnA, count(distinct columnB)
From TableA
group by ColumnA

is this what you need?
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

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"
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.
Avatar of rhservan

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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)
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.
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?
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