Solved

Is this a correct use of UNION?

Posted on 2013-05-28
11
305 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

856 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