Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Select partitions of rows where columns have equal value

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

| column_1   | column_2 | column_3 |
----------------------------------
|'irrelevant'|    1     |    5     |
|'irrelevant'|    1     |    5     |
|'irrelevant'|    1     |    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     |

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

>Solution :

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
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading