• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 37
  • 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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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