I have a table with 3 columns: id, val1 and val2.
For each distinct value of val2 I want to select all rows for which multiple distinct values of val1 exist.
Example:
| id | val1 | val2 |
|------------------|
| 1 | A1 | a2 |
| 2 | A1 | a2 |
| 3 | A1 | b2 |
| 4 | B1 | b2 |
| 5 | A1 | c2 |
| 6 | A1 | c2 |
| 7 | A1 | c2 |
| 8 | A1 | d2 |
| 9 | C1 | d2 |
| 10 | A1 | d2 |
Desired result:
| id | val1 | val2 |
|------------------|
| 3 | A1 | b2 |
| 4 | B1 | b2 |
| 8 | A1 | d2 |
| 9 | C1 | d2 |
| 10 | A1 | d2 |
I did not manage to come up with any query which allows me to do this, maybe someone else has an idea on how to solve this.
You could use a having clause to search for val2 with more than one distinct value of val1. For example:
select yt.*
from YourTable yt
join (
select val2
from YourTable
group by
val2
having count(distinct val1) > 1
) as filter
on yt.val2 = filter.val2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With