I have two columns for example, I want to list all the rows where the items in ROW A appears more than once in Column A, but have distinct values in Column B. So far I have not figured out an answer
Column A Column B
Apple x
Banana a
Orange b
Orange b
Banana x
Avocado d
Try this query:
SELECT ColumnA
FROM table t
GROUP BY ColumnA
HAVING COUNT(DISTINCT ColumnB) >= 2;
An alternative HAVING
clause that might be more efficient is:
HAVING MIN(ColumnB) <> MAX(ColumnB)
Try this query:
SELECT ColumnA
FROM mytable
GROUP BY ColumnA
HAVING COUNT(*) > 1 AND COUNT(DISTINCT ColumnB) = COUNT(*)
HAVING
clause is true if ColumnA
value appears more than once.ColumnB
are distinct within a ColumnA
group.SQL Fiddle Demo
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