Solved

Counting Distinct values in two columns

Posted on 2016-11-15
3
29 Views
Last Modified: 2016-11-15
Hello

I'm using SQL Server 2008 R2.  I have a table with two columns with similar information.  In some rows (not all), the information in Column 1 was erroneously switched with the information in Column 2 and vice versa (the data were received this way).  

I need to count the distinct values that exist in both Column 1 and Column 2.  

e.g., given this:
--------------------------------
Column 1 | Column 2
-------------------------------
    A121    |  A121
    B111    |  C111
    D111    |  A121

The distinct count would be 4
0
Comment
Question by:ttist25
[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
  • 2
3 Comments
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41888344
select col1 from yourtable
union
select col2 from yourtable
0
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41888350
select count(*) from
(
select col1 from yourtable
union
select col2 from yourtable
)u
0
 
LVL 1

Author Closing Comment

by:ttist25
ID: 41888360
Perfect Pawan!  Thanks so much.  That got to the number I thought it should be.  

Thanks!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL trigger 5 32
How can I find this data? 3 25
SQL works but want to get the XML node data separately 11 28
denied execute as 13 28
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…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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