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 rows with distinct values for columns in SQL

I have a SQL table containing 3 columns – LocationA, LocationB, LocationC

How do I return the rows for which there is a difference in the values?

For example,

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

LocationA, LocationB, LocationC
US, US, US ---------------> Row1
US, IN, US ---------------> Row2
IN, US, IN ---------------> Row3

I need to return Row2 and Row3 because these rows show 2 different values.

>Solution :

Don’t forget to use ISNULL if the columns are nullable :

WHERE ISNULL(LocationA,'NULL') <> ISNULL(LocationB,'NULL') 
    OR ISNULL(LocationB,'NULL') <> ISNULL(LocationC,'NULL') 
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