Solved

Counting Distinct values in two columns

Posted on 2016-11-15
3
13 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
  • 2
3 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
select col1 from yourtable
union
select col2 from yourtable
0
 
LVL 17

Accepted Solution

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

Author Closing Comment

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

Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 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

10 Experts available now in Live!

Get 1:1 Help Now