?
Solved

Counting Distinct values in two columns

Posted on 2016-11-15
3
Medium Priority
?
32 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 29

Expert Comment

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

Accepted Solution

by:
Pawan Kumar earned 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

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…
SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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