Solved

Counting Distinct values in two columns

Posted on 2016-11-15
3
27 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Designing and Implementing a Data Warehouse 3 35
Query to return total 6 18
SQL BULK INSERT Comma Delimited Issue 8 48
SSRS 2012 r2 - Parm Drop Down has Date/Time 12 31
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

786 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