Home > database >  Select rows with distinct values for columns in SQL
Select rows with distinct values for columns in SQL

Time:10-26

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,

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.

CodePudding user response:

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') 
  • Related