• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 38
  • Last Modified:

Counting Distinct values in two columns

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
ttist25
Asked:
ttist25
  • 2
1 Solution
 
Pawan KumarDatabase ExpertCommented:
select col1 from yourtable
union
select col2 from yourtable
0
 
Pawan KumarDatabase ExpertCommented:
select count(*) from
(
select col1 from yourtable
union
select col2 from yourtable
)u
0
 
ttist25Author Commented:
Perfect Pawan!  Thanks so much.  That got to the number I thought it should be.  

Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now