Solved

Is this a correct use of UNION?

Posted on 2013-05-28
11
302 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
  • 5
  • 3
  • 2
  • +1
11 Comments
 
LVL 48

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 48

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
 
LVL 48

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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 48

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 48

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL 2012 Syntax Error 5 24
Help with SQL Query 23 39
encyps queries mssql 15 26
Sql query 34 18
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 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

14 Experts available now in Live!

Get 1:1 Help Now