0

I have data grouped (column_2) and I want to select only the rows where for each group (same column_2 value) all the values at another column (column_3) is equal. There are more columns with no distinct values (such as column_1) but we don't care about their values.

For example, for this table:

| column_1   | column_2 | column_3 |
----------------------------------
|'irrelevant'|    1     |    5     |
|'irrelevant'|    1     |    5     |
|'irrelevant'|    1     |    5     |
|'irrelevant'|    2     |    3     |
|'irrelevant'|    2     |    5     |
|'irrelevant'|    2     |    5     |
|'irrelevant'|    3     |    7     |
|'irrelevant'|    3     |    7     |
|'irrelevant'|    4     |    8     |
|'irrelevant'|    4     |    9     |
|'irrelevant'|    4     |    2     |
|'irrelevant'|    5     |    6     |
|'irrelevant'|    6     |    2     |
|'irrelevant'|    7     |    1     |
|'irrelevant'|    7     |    1     |
|'irrelevant'|    7     |    1     |

I'd get:

| column_1   | column_2 | column_3 |
----------------------------------
|'irrelevant'|    1     |    5     |
|'irrelevant'|    1     |    5     |
|'irrelevant'|    1     |    5     |
|'irrelevant'|    3     |    7     |
|'irrelevant'|    3     |    7     |
|'irrelevant'|    5     |    6     |
|'irrelevant'|    6     |    2     |
|'irrelevant'|    7     |    1     |
|'irrelevant'|    7     |    1     |
|'irrelevant'|    7     |    1     |

The rows are skipped where, for the same column_2 value, the column_3 is not the same across them.

1
  • 1
    Why is group 4 in the expected result? Commented Apr 27, 2022 at 13:32

1 Answer 1

1

You can use COUNT(DISTINCT <expr>) to find out how many different values are there per group.

For example:

select *
from t
where column_2 in (
  select column_2 
  from t 
  group by column_2 
  having count(distinct column_3) = 1
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I didn't know I could do count(distinct <column_name>)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.