Andy
Andy

Reputation: 8562

Find all rows same value in Col1 but different values in Col2

Given a table similar to this:

Col1                 Col2
----                 ----
A                    A
A                    A
B                    B
C                    C
C                    D

I'm trying to write a query which will identify all values in Col1 which appear more than once AND have differing values in Col2. So a query that would return only rows with C in Col1 (because there are two rows with C in Col1, and they have differing values in Col2).

Upvotes: 0

Views: 332

Answers (1)

juergen d
juergen d

Reputation: 204766

Groupy by col1 and take only the ones having more than 1 unique col2. These automatically have more than one col1 value too.

select col1
from your_table
group by col1
having count(distinct col2) > 1

Upvotes: 3

Related Questions