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

How to write SQL for finding columns with matching string

I do have a following table

TableA
| Column A | Column B |
| ——– | ——– |
| 1234 | Y |
| 2345 | N |
| 3456 | Y |
| 3456 | Y |
| 3456 | N |
| 2345 | N |
| 1234 | N |
| 2345 | N |

Here, ‘1234’ and ‘3456’ has values Y and N whereas 2345 has only value N

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

I want to display values of column A where there are 2 values (Y and N) in Column B. Ideally
| Column A | Column B |
| ——– | ——– |
| 1234 | Y |
| 1234 | N |
| 3456 | Y |
| 3456 | N |

I tried using

Select * from TableA
where column b = ‘Y’
and column b = ‘N’

but it doesn’t give desired result.

>Solution :

I prefer aggregation here:

SELECT ColumnA
FROM TableA
GROUP BY ColumnA
HAVING MIN(ColumnB) <> MAX(ColumnB);

The above assumes that the only two values which would ever appear in ColumnB are Y and N.

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