Home > Enterprise >  How to write SQL for finding columns with matching string
How to write SQL for finding columns with matching string

Time:11-04

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

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.

CodePudding user response:

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.

  • Related